Line data Source code
1 : package education
2 :
3 : import (
4 : "context"
5 :
6 : "resume/internal/shared/apperr"
7 : "resume/internal/shared/valerr"
8 : )
9 :
10 : // ReorderEducation は 認証済みユーザーの学歴 を 並び順を変更 します
11 : func (uc *Interactor) ReorderEducation(
12 : ctx context.Context,
13 : in ReorderInput,
14 0 : ) (ReorderOutput, error) {
15 0 : if err := uc.tx.Do(ctx, func(txCtx context.Context) error {
16 0 : // ここでEducationIDsが1個でもuserIDの物で無かったらエラーを返したい
17 0 : // 1. ユーザーが所有している学歴を全て取得
18 0 : ownedList, err := uc.ueRepo.ListByUserID(txCtx, in.UserID)
19 0 : if err != nil {
20 0 : return err
21 0 : }
22 : // 2. 所有しているIDのマップを作成(検索を高速化するため)
23 0 : ownedIDMap := make(map[uint64]bool)
24 0 : for _, edu := range ownedList {
25 0 : ownedIDMap[edu.ID] = true
26 0 : }
27 : // 3. リクエストされたIDが全てユーザーの物であるかチェック
28 0 : for _, reqID := range in.EducationIDs {
29 0 : if !ownedIDMap[reqID] {
30 0 : // 所有していないIDが含まれている場合はエラー
31 0 : ve := valerr.New()
32 0 : ve.Add(
33 0 : "domain.userEducation.id",
34 0 : "validation.rules.invalid",
35 0 : map[string]any{
36 0 : "field": "domain.userEducation.id",
37 0 : "value": reqID,
38 0 : },
39 0 : )
40 0 : return apperr.New(
41 0 : apperr.CodeUnprocessable,
42 0 : "The request contains semantically invalid data.",
43 0 : ve.ToDetails(),
44 0 : )
45 0 : }
46 : }
47 : // 4. チェックを通過したら並び替え実行
48 0 : return uc.ueRepo.UpdateOrder(txCtx, in.UserID, in.EducationIDs)
49 0 : }); err != nil {
50 0 : return ReorderOutput{}, err
51 0 : }
52 0 : return ReorderOutput{}, nil
53 : }
|