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
16 changes: 16 additions & 0 deletions api/externals/handler/user_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,19 @@ func (h *Handler) GetCurrentUser(c echo.Context, params generated.GetCurrentUser
return c.JSON(http.StatusOK, user)
}
}

// ユーザーに紐づくグループの更新
func (h *Handler) UpdateUserGroups(c echo.Context, userId int, year int) error {
var updateUserGroupsRequest generated.UpdateUserGroupsRequest
if err := c.Bind(&updateUserGroupsRequest); err != nil {
return err
}
Comment thread
Wakai111 marked this conversation as resolved.
if updateUserGroupsRequest.GroupIds == nil {
return c.String(http.StatusBadRequest, "groupIds is required")
}
updatedUserGroups, err := h.userUseCase.UpdateUserGroups(c.Request().Context(), userId, year, updateUserGroupsRequest.GroupIds)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
return c.JSON(http.StatusOK, updatedUserGroups)
}
73 changes: 73 additions & 0 deletions api/externals/repository/user_group_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package repository

import (
"context"
"database/sql"

"github.com/NUTFes/FinanSu/api/drivers/db"
"github.com/NUTFes/FinanSu/api/externals/repository/abstract"
goqu "github.com/doug-martin/goqu/v9"
_ "github.com/doug-martin/goqu/v9/dialect/mysql"
)

type userGroupRepository struct {
client db.Client
crud abstract.Crud
}

type UserGroupRepository interface {
BulkInsert(context.Context, *sql.Tx, int, []int) error
BulkDelete(context.Context, *sql.Tx, int, []int) error
}

func NewUserGroupRepository(client db.Client, crud abstract.Crud) UserGroupRepository {
return &userGroupRepository{
client: client,
crud: crud,
}
}

// 指定された (user_id, group_id) を登録。
func (r *userGroupRepository) BulkInsert(ctx context.Context, tx *sql.Tx, userID int, insertGroupIDs []int) error {
if len(insertGroupIDs) == 0 {
return nil
}

// レコード作成
var userGroupRecords []goqu.Record
for _, groupID := range insertGroupIDs {
userGroupRecords = append(userGroupRecords, goqu.Record{
"user_id": userID,
"group_id": groupID,
})
}

queryDataset := goqu.Dialect("mysql").Insert("user_groups").Rows(userGroupRecords)
query, args, err := queryDataset.ToSQL()
if err != nil {
return err
}

_, err = tx.ExecContext(ctx, query, args...)
return err
}

// 指定された deleteGroupIDs を削除。
func (r *userGroupRepository) BulkDelete(ctx context.Context, tx *sql.Tx, userID int, deleteGroupIDs []int) error {
if len(deleteGroupIDs) == 0 {
return nil
}

queryDataset := goqu.Dialect("mysql").Delete("user_groups").Where(
goqu.I("user_id").Eq(userID),
goqu.I("group_id").In(deleteGroupIDs),
)

query, args, err := queryDataset.ToSQL()
if err != nil {
return err
}

_, err = tx.ExecContext(ctx, query, args...)
return err
}
1 change: 1 addition & 0 deletions api/externals/repository/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var RepositoryProviderSet = wire.NewSet(
NewTeacherRepository,
NewTransactionRepository,
NewUserRepository,
NewUserGroupRepository,
NewYearRepository,
NewSponsorshipActivityRepository,
)
43 changes: 43 additions & 0 deletions api/generated/openapi_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion api/internals/di/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions api/internals/domain/user_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,38 @@ type UserGroup struct {
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}

type GroupIDs []int

// 現在の所属グループと、リクエストされたグループを比較し、削除すべきIDリストと、追加すべきIDリストを計算して返す
func (existingGroupIDs GroupIDs) Diff(requestedGroupIDs GroupIDs) (groupIDsToDelete []int, groupIDsToInsert []int) {

// map の準備
existGroupIDMap := make(map[int]struct{}, len(existingGroupIDs))
requestGroupIDMap := make(map[int]struct{}, len(requestedGroupIDs))

// 現在の所属グループを map に詰め込む
for _, id := range existingGroupIDs {
existGroupIDMap[id] = struct{}{}
}
// リクエストされたグループを map に詰め込む
for _, id := range requestedGroupIDs {
requestGroupIDMap[id] = struct{}{}
}

// 削除すべきIDを探す
for id := range existGroupIDMap {
if _, ok := requestGroupIDMap[id]; !ok {
groupIDsToDelete = append(groupIDsToDelete, id)
}
}
// 追加すべきIDを探す
for id := range requestGroupIDMap {
if _, ok := existGroupIDMap[id]; !ok {
groupIDsToInsert = append(groupIDsToInsert, id)
}
}

// 返却
return groupIDsToDelete, groupIDsToInsert
}
Loading
Loading