Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions internal/handlers/achievement_handler.go
Original file line number Diff line number Diff line change
@@ -1 +1,152 @@
package handlers

import (
"context"
"net/http"
"strconv"

"github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
"theaesthetics.ru/base/internal/models"
"theaesthetics.ru/base/internal/services"
)

type AchievementsHandler struct {
service *services.AchievementsService
}

func NewAhievementsHandler(service *services.AchievementsService) *AchievementsHandler {
return &AchievementsHandler{service: service}
}

func (h *AchievementsHandler) GetAllAchievements(c echo.Context) error {
ctx := c.Request().Context()
achievements, err := h.service.GetAllAchievements(ctx)
if err != nil {
logrus.Error(err)

return c.JSON(http.StatusInternalServerError, map[string]string{
"error": err.Error(),
})
}

return c.JSON(http.StatusOK, achievements)
}

func (h *AchievementsHandler) GetAchievementById(c echo.Context) error {
ctx := c.Request().Context()
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
logrus.Error(err)

return c.JSON(http.StatusBadRequest, map[string]string{
"error": err.Error(),
})
}

var idu8 uint8 = uint8(id)
achievement, err := h.service.GetAchievementById(ctx, idu8)

if err != nil {
logrus.Error(err)

return c.JSON(http.StatusBadRequest, map[string]string{
"error": err.Error(),
})
}
return c.JSON(http.StatusOK, achievement)
}

func (h *AchievementsHandler) CreateAchievement(c echo.Context) error {
ctx := context.Background()
var achieve models.Achievenment
err := c.Bind(&achieve)

if err != nil {
logrus.Error(err)

return c.JSON(http.StatusBadRequest, map[string]string{
"error": err.Error(),
})
}

err = h.service.CreateAchievement(ctx, achieve)

if err != nil {
logrus.Error(err)

return c.JSON(http.StatusBadRequest, map[string]string{
"error": err.Error(),
})
}

return c.JSON(http.StatusCreated, map[string]string{
"message": "created",
})
}

func (h *AchievementsHandler) DeleteAchievement(c echo.Context) error {
ctx := context.Background()
id, err := strconv.Atoi(c.Param("id"))

if err != nil {
logrus.Error(err)

return c.JSON(http.StatusBadRequest, map[string]string{
"error": err.Error(),
})
}
uid := uint8(id)
err = h.service.DeleteAchievement(ctx, uid)

if err != nil {
logrus.Error(err)

return c.JSON(http.StatusBadRequest, map[string]string{
"error": err.Error(),
})
}

return c.JSON(http.StatusOK, map[string]string{
"message": "deleted",
})
}

func (h *AchievementsHandler) UpdateAchievement(c echo.Context) error {
ctx := context.Background()
id, err := strconv.Atoi(c.Param("id"))

if err != nil {
logrus.Error(err)

return c.JSON(http.StatusBadRequest, map[string]string{
"error": err.Error(),
})
}
uid := uint8(id)

achieve := models.Achievenment{Id: uid}
err = c.Bind(&achieve)

if err != nil {
logrus.Error(err)

return c.JSON(http.StatusBadRequest, map[string]string{
"error": err.Error(),
})
}

err = h.service.UpdateAchievement(ctx, achieve)

if err != nil {
logrus.Error(err)

return c.JSON(http.StatusInternalServerError, map[string]string{
"error": err.Error(),
})
}

return c.JSON(http.StatusCreated, map[string]string{
"message": "updated",
})
}
115 changes: 115 additions & 0 deletions internal/handlers/exercise_handler.go
Original file line number Diff line number Diff line change
@@ -1 +1,116 @@
package handlers

import (
"context"
"net/http"
"strconv"

"github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
"theaesthetics.ru/base/internal/models"
"theaesthetics.ru/base/internal/services"
)

type ExerciseHandler struct {
service *services.ExercisesService
}

func NewExerciseHandler(service *services.ExercisesService) *ExerciseHandler {
return &ExerciseHandler{service: service}
}

func (h *ExerciseHandler) GetAllExercises(c echo.Context) error {
ctx := context.Background()
exercises, err := h.service.GetAllExercises(ctx)
if err != nil {
logrus.WithError(err).Error("Failed to get exercises")
return respondWithError(c, http.StatusInternalServerError, err)
}

return c.JSON(http.StatusOK, exercises)
}

func (h *ExerciseHandler) GetExerciseById(c echo.Context) error {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
logrus.WithError(err).Error("Invalid exercise ID")
return respondWithError(c, http.StatusBadRequest, err)
}
uid := uint8(id)

ctx := context.Background()
exercise, err := h.service.GetExerciseById(ctx, uid)
if err != nil {
logrus.WithError(err).Error("Exercise not found")
return respondWithError(c, http.StatusNotFound, err)
}

return c.JSON(http.StatusOK, exercise)
}

func (h *ExerciseHandler) CreateExercise(c echo.Context) error {
ctx := context.Background()

var exercise models.FullExercises
if err := c.Bind(&exercise); err != nil {
logrus.WithError(err).Error("Failed to bind request data")
return respondWithError(c, http.StatusBadRequest, err)
}

id, err := h.service.CreateExercise(ctx, exercise)
if err != nil {
logrus.WithError(err).Error("Failed to create exercise")
return respondWithError(c, http.StatusInternalServerError, err)
}

return c.JSON(http.StatusCreated, map[string]interface{}{
"message": "Exercise created successfully",
"id": id,
})
}

func (h *ExerciseHandler) UpdateExercise(c echo.Context) error {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
logrus.WithError(err).Error("Invalid exercise ID")
return respondWithError(c, http.StatusBadRequest, err)
}
uid := uint8(id)

ctx := context.Background()

var exercise models.FullExercises
if err := c.Bind(&exercise); err != nil {
logrus.WithError(err).Error("Failed to bind request data")
return respondWithError(c, http.StatusBadRequest, err)
}

if err := h.service.UpdateExercise(ctx, uid, exercise); err != nil {
logrus.WithError(err).Error("Failed to update exercise")
return respondWithError(c, http.StatusInternalServerError, err)
}

return c.JSON(http.StatusOK, map[string]string{
"message": "Exercise updated successfully",
})
}

func (h *ExerciseHandler) DeleteExercise(c echo.Context) error {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
logrus.WithError(err).Error("Invalid exercise ID")
return respondWithError(c, http.StatusBadRequest, err)
}
uid := uint8(id)

ctx := context.Background()

if err := h.service.DeleteExercise(ctx, uid); err != nil {
logrus.WithError(err).Error("Failed to delete exercise")
return respondWithError(c, http.StatusInternalServerError, err)
}

return c.JSON(http.StatusOK, map[string]string{
"message": "Exercise deleted successfully",
})
}
65 changes: 65 additions & 0 deletions internal/handlers/user_handler.go
Original file line number Diff line number Diff line change
@@ -1 +1,66 @@
package handlers

import (
"context"
"net/http"
"strconv"

"github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
"theaesthetics.ru/base/internal/services"
)

type UserHandler struct {
service *services.UserService
}

func NewUserHandler(service *services.UserService) *UserHandler {
return &UserHandler{service: service}
}

func (h *UserHandler) GetAllUsers(c echo.Context) error {
ctx := context.Background()
users, err := h.service.GetAllUsers(ctx)
if err != nil {
logrus.WithError(err).Error("Failed to get users")
return respondWithError(c, http.StatusInternalServerError, err)
}

return c.JSON(http.StatusOK, users)
}

func (h *UserHandler) GetUserById(c echo.Context) error {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
logrus.WithError(err).Error("Invalid user ID")
return respondWithError(c, http.StatusBadRequest, err)
}
uid := uint(id)

ctx := context.Background()
user, err := h.service.GetUserById(ctx, uid)
if err != nil {
logrus.WithError(err).Error("Failed to get user")
return respondWithError(c, http.StatusInternalServerError, err)
}

return c.JSON(http.StatusOK, user)
}

func (h *UserHandler) GetUserAchievements(c echo.Context) error {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
logrus.WithError(err).Error("Invalid user ID")
return respondWithError(c, http.StatusBadRequest, err)
}
uid := uint(id)

ctx := context.Background()
achievements, err := h.service.GetUserAchievements(ctx, uid)
if err != nil {
logrus.WithError(err).Error("Failed to get achievements")
return respondWithError(c, http.StatusInternalServerError, err)
}

return c.JSON(http.StatusOK, achievements)
}
7 changes: 7 additions & 0 deletions internal/models/achievement.go
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
package models

type Achievenment struct {
Id uint8 `json:"id"`
Title string `json:"title"`
Description string `json:"descripton"`
Image string `json:"image"`
}
29 changes: 29 additions & 0 deletions internal/models/exercise.go
Original file line number Diff line number Diff line change
@@ -1 +1,30 @@
package models

type Exercises struct {
Id uint8 `json:"id"`
Title string `json:"title"`
Image string `json:"image"`
Description string `json:"descripton"`
Video_url string `json:"video_url"`
EquipmentId uint8 `json:"equipment_id"`
Sets uint8 `json:"Sets"`
Reps uint8 `json:"Reps"`
Difficult uint8 `json:"Difficult"`
Lead_muscle_id uint8 `json:"lead_muscle_id"`
}

type FullExercises struct {
Id uint8 `json:"id"`
Title string `json:"title"`

Image string `json:"image"`
Description string `json:"descripton"`
Video_url string `json:"video_url"`

Sets uint8 `json:"sets"`
Reps uint8 `json:"reps"`
Difficult uint8 `json:"difficult"`

Equipment Equipment `json:"equipment"`
Muscles Muscles `json:"muscle"`
}
Loading