Skip to content

Commit

Permalink
SAC-19 User Tag CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
Alder Whiteford authored and Alder Whiteford committed Jan 26, 2024
1 parent f000888 commit 498a888
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 1 deletion.
22 changes: 22 additions & 0 deletions backend/src/controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,25 @@ func (u *UserController) DeleteUser(c *fiber.Ctx) error {

return c.SendStatus(fiber.StatusNoContent)
}

func (u *UserController) GetUserTags(c *fiber.Ctx) error {
tags, err := u.userService.GetUserTags(c.Params("uid"))
if err != nil {
return err.FiberError(c)
}
return c.Status(fiber.StatusOK).JSON(&tags)
}

func (u *UserController) CreateUserTags(c *fiber.Ctx) error {
var requestBody models.CreateUserTagsBody
if err := c.BodyParser(&requestBody); err != nil {
return errors.FailedToParseRequestBody.FiberError(c)
}

tags, err := u.userService.CreateUserTags(c.Params("uid"), requestBody.Tags)
if err != nil {
return err.FiberError(c)
}

return c.Status(fiber.StatusCreated).JSON(&tags)
}
8 changes: 7 additions & 1 deletion backend/src/models/user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package models

import "github.com/GenerateNU/sac/backend/src/types"
import (
"github.com/GenerateNU/sac/backend/src/types"
)

type UserRole string

Expand Down Expand Up @@ -76,3 +78,7 @@ type UpdateUserRequestBody struct {
College College `json:"college" validate:"omitempty,oneof=CAMD DMSB KCCS CE BCHS SL CPS CS CSSH"`
Year Year `json:"year" validate:"omitempty,min=1,max=6"`
}

type CreateUserTagsBody struct {
Tags []uint `json:"tags" validate:"required"`
}
5 changes: 5 additions & 0 deletions backend/src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ func userRoutes(router fiber.Router, userService services.UserServiceInterface)
users.Get("/:id", userController.GetUser)
users.Patch("/:id", userController.UpdateUser)
users.Delete("/:id", userController.DeleteUser)

userTags := users.Group("/:uid/tags")

userTags.Post("/", userController.CreateUserTags)
userTags.Get("/", userController.GetUserTags)
}

func categoryRoutes(router fiber.Router, categoryService services.CategoryServiceInterface) {
Expand Down
25 changes: 25 additions & 0 deletions backend/src/services/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type UserServiceInterface interface {
GetUser(id string) (*models.User, *errors.Error)
UpdateUser(id string, userBody models.UpdateUserRequestBody) (*models.User, *errors.Error)
DeleteUser(id string) *errors.Error
GetUserTags(id string) ([]models.Tag, *errors.Error)
CreateUserTags(id string, tagIDs []uint) ([]models.Tag, *errors.Error)
}

type UserService struct {
Expand Down Expand Up @@ -107,3 +109,26 @@ func (u *UserService) DeleteUser(id string) *errors.Error {

return transactions.DeleteUser(u.DB, *idAsInt)
}

func (u *UserService) GetUserTags(id string) ([]models.Tag, *errors.Error) {
idAsInt, err := utilities.ValidateID(id)
if err != nil {
return nil, err
}

return transactions.GetUserTags(u.DB, *idAsInt)
}

func (u *UserService) CreateUserTags(id string, tagIDs []uint) ([]models.Tag, *errors.Error) {
// Validate the id:
idAsInt, err := utilities.ValidateID(id)
if err != nil {
return nil, err
}

// Retrieve a list of valid tags from the ids:
tags, err := transactions.GetTagsByIDs(u.DB, tagIDs)

// Update the user to reflect the new tags:
return transactions.CreateUserTags(u.DB, *idAsInt, tags)
}
12 changes: 12 additions & 0 deletions backend/src/transactions/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,15 @@ func DeleteTag(db *gorm.DB, id uint) *errors.Error {

return nil
}

func GetTagsByIDs(db *gorm.DB, selectedTagIDs []uint) ([]models.Tag, *errors.Error) {
if len(selectedTagIDs) != 0 {
var tags []models.Tag
if err := db.Model(models.Tag{}).Where("id IN ?", selectedTagIDs).Find(&tags).Error; err != nil {
return nil, &errors.FailedToGetTag
}

return tags, nil
}
return []models.Tag{}, nil
}
27 changes: 27 additions & 0 deletions backend/src/transactions/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,30 @@ func DeleteUser(db *gorm.DB, id uint) *errors.Error {
}
return nil
}

func GetUserTags(db *gorm.DB, id uint) ([]models.Tag, *errors.Error) {
var tags []models.Tag

user, err := GetUser(db, id)
if err != nil {
return nil, &errors.UserNotFound
}

if err := db.Model(&user).Association("Tag").Find(&tags) ; err != nil {
return nil, &errors.FailedToGetTag
}
return tags, nil
}

func CreateUserTags(db *gorm.DB, id uint, tags []models.Tag) ([]models.Tag, *errors.Error) {
user, err := GetUser(db, id)
if err != nil {
return nil, &errors.UserNotFound
}

if err := db.Model(&user).Association("Tag").Replace(tags); err != nil {
return nil, &errors.FailedToUpdateUser
}

return tags, nil
}

0 comments on commit 498a888

Please sign in to comment.