-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a54d3a
commit 24c948e
Showing
40 changed files
with
1,142 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package controller | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/stonith404/pocket-id/backend/internal/common" | ||
"github.com/stonith404/pocket-id/backend/internal/dto" | ||
"github.com/stonith404/pocket-id/backend/internal/middleware" | ||
"github.com/stonith404/pocket-id/backend/internal/service" | ||
"github.com/stonith404/pocket-id/backend/internal/utils" | ||
) | ||
|
||
func NewUserGroupController(group *gin.RouterGroup, jwtAuthMiddleware *middleware.JwtAuthMiddleware, userGroupService *service.UserGroupService) { | ||
ugc := UserGroupController{ | ||
UserGroupService: userGroupService, | ||
} | ||
|
||
group.GET("/user-groups", jwtAuthMiddleware.Add(true), ugc.list) | ||
group.GET("/user-groups/:id", jwtAuthMiddleware.Add(true), ugc.get) | ||
group.POST("/user-groups", jwtAuthMiddleware.Add(true), ugc.create) | ||
group.PUT("/user-groups/:id", jwtAuthMiddleware.Add(true), ugc.update) | ||
group.DELETE("/user-groups/:id", jwtAuthMiddleware.Add(true), ugc.delete) | ||
group.PUT("/user-groups/:id/users", jwtAuthMiddleware.Add(true), ugc.updateUsers) | ||
} | ||
|
||
type UserGroupController struct { | ||
UserGroupService *service.UserGroupService | ||
} | ||
|
||
func (ugc *UserGroupController) list(c *gin.Context) { | ||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) | ||
pageSize, _ := strconv.Atoi(c.DefaultQuery("limit", "10")) | ||
searchTerm := c.Query("search") | ||
|
||
groups, pagination, err := ugc.UserGroupService.List(searchTerm, page, pageSize) | ||
if err != nil { | ||
utils.ControllerError(c, err) | ||
return | ||
} | ||
|
||
var groupsDto = make([]dto.UserGroupDtoWithUserCount, len(groups)) | ||
for i, group := range groups { | ||
var groupDto dto.UserGroupDtoWithUserCount | ||
if err := dto.MapStruct(group, &groupDto); err != nil { | ||
utils.ControllerError(c, err) | ||
return | ||
} | ||
groupDto.UserCount, err = ugc.UserGroupService.GetUserCountOfGroup(group.ID) | ||
if err != nil { | ||
utils.ControllerError(c, err) | ||
return | ||
} | ||
groupsDto[i] = groupDto | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"data": groupsDto, | ||
"pagination": pagination, | ||
}) | ||
} | ||
|
||
func (ugc *UserGroupController) get(c *gin.Context) { | ||
group, err := ugc.UserGroupService.Get(c.Param("id")) | ||
if err != nil { | ||
utils.ControllerError(c, err) | ||
return | ||
} | ||
|
||
var groupDto dto.UserGroupDtoWithUsers | ||
if err := dto.MapStruct(group, &groupDto); err != nil { | ||
utils.ControllerError(c, err) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, groupDto) | ||
} | ||
|
||
func (ugc *UserGroupController) create(c *gin.Context) { | ||
var input dto.UserGroupCreateDto | ||
if err := c.ShouldBindJSON(&input); err != nil { | ||
utils.ControllerError(c, err) | ||
return | ||
} | ||
|
||
group, err := ugc.UserGroupService.Create(input) | ||
if err != nil { | ||
if errors.Is(err, common.ErrNameAlreadyInUse) { | ||
utils.CustomControllerError(c, http.StatusConflict, err.Error()) | ||
} else { | ||
utils.ControllerError(c, err) | ||
} | ||
return | ||
} | ||
|
||
var groupDto dto.UserGroupDtoWithUsers | ||
if err := dto.MapStruct(group, &groupDto); err != nil { | ||
utils.ControllerError(c, err) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusCreated, groupDto) | ||
} | ||
|
||
func (ugc *UserGroupController) update(c *gin.Context) { | ||
var input dto.UserGroupCreateDto | ||
if err := c.ShouldBindJSON(&input); err != nil { | ||
utils.ControllerError(c, err) | ||
return | ||
} | ||
|
||
group, err := ugc.UserGroupService.Update(c.Param("id"), input) | ||
if err != nil { | ||
if errors.Is(err, common.ErrNameAlreadyInUse) { | ||
utils.CustomControllerError(c, http.StatusConflict, err.Error()) | ||
} else { | ||
utils.ControllerError(c, err) | ||
} | ||
return | ||
} | ||
|
||
var groupDto dto.UserGroupDtoWithUsers | ||
if err := dto.MapStruct(group, &groupDto); err != nil { | ||
utils.ControllerError(c, err) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, groupDto) | ||
} | ||
|
||
func (ugc *UserGroupController) delete(c *gin.Context) { | ||
if err := ugc.UserGroupService.Delete(c.Param("id")); err != nil { | ||
utils.ControllerError(c, err) | ||
return | ||
} | ||
|
||
c.Status(http.StatusNoContent) | ||
} | ||
|
||
func (ugc *UserGroupController) updateUsers(c *gin.Context) { | ||
var input dto.UserGroupUpdateUsersDto | ||
if err := c.ShouldBindJSON(&input); err != nil { | ||
utils.ControllerError(c, err) | ||
return | ||
} | ||
|
||
group, err := ugc.UserGroupService.UpdateUsers(c.Param("id"), input) | ||
if err != nil { | ||
utils.ControllerError(c, err) | ||
return | ||
} | ||
|
||
var groupDto dto.UserGroupDtoWithUsers | ||
if err := dto.MapStruct(group, &groupDto); err != nil { | ||
utils.ControllerError(c, err) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, groupDto) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package dto | ||
|
||
import "time" | ||
|
||
type UserGroupDtoWithUsers struct { | ||
ID string `json:"id"` | ||
FriendlyName string `json:"friendlyName"` | ||
Name string `json:"name"` | ||
Users []UserDto `json:"users"` | ||
CreatedAt time.Time `json:"createdAt"` | ||
} | ||
|
||
type UserGroupDtoWithUserCount struct { | ||
ID string `json:"id"` | ||
FriendlyName string `json:"friendlyName"` | ||
Name string `json:"name"` | ||
UserCount int64 `json:"userCount"` | ||
CreatedAt time.Time `json:"createdAt"` | ||
} | ||
|
||
type UserGroupCreateDto struct { | ||
FriendlyName string `json:"friendlyName" binding:"required,min=3,max=30"` | ||
Name string `json:"name" binding:"required,min=3,max=30,userGroupName"` | ||
} | ||
|
||
type UserGroupUpdateUsersDto struct { | ||
UserIDs []string `json:"userIds" binding:"required"` | ||
} | ||
|
||
type AssignUserToGroupDto struct { | ||
UserID string `json:"userId" binding:"required"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package model | ||
|
||
type UserGroup struct { | ||
Base | ||
FriendlyName string | ||
Name string `gorm:"unique"` | ||
Users []User `gorm:"many2many:user_groups_users;"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.