Line data Source code
1 : // Package license は 資格・免許 に関するユースケース(アプリケーションロジック)を提供します
2 : // ドメイン層のエンティティやリポジトリを操作し、アプリケーション全体で利用可能なビジネスフローを実装します
3 : package license
4 :
5 : import (
6 : "context"
7 :
8 : "resume/internal/shared/apperr"
9 : "resume/internal/shared/valerr"
10 : )
11 :
12 : // DeleteUserLicense は指定された資格・免許を削除するユースケースを実装します
13 : func (uc *Interactor) DeleteUserLicense(
14 : ctx context.Context,
15 : in DeleteUserLicenseInput,
16 0 : ) (DeleteUserLicenseOutput, error) {
17 0 : if err := uc.tx.Do(ctx, func(txCtx context.Context) error {
18 0 :
19 0 : current, err := uc.ulRepo.FindByID(txCtx, in.ID)
20 0 : if err != nil {
21 0 : return err
22 0 : }
23 0 : if current == nil {
24 0 : return apperr.New(
25 0 : apperr.CodeNotFound,
26 0 : "user license not found",
27 0 : map[string]any{
28 0 : "id": in.ID,
29 0 : },
30 0 : )
31 0 : }
32 0 : if current.UserID != in.UserID {
33 0 : ve := valerr.New()
34 0 : ve.Add(
35 0 : "domain.userLicense.id",
36 0 : "validation.rules.invalid",
37 0 : map[string]any{
38 0 : "field": "domain.userLicense.id",
39 0 : "value": in.ID,
40 0 : },
41 0 : )
42 0 : return apperr.New(
43 0 : apperr.CodeUnprocessable,
44 0 : "The Request contains semantically invalid data",
45 0 : ve.ToDetails(),
46 0 : )
47 0 : }
48 :
49 0 : if err := uc.ulRepo.Delete(txCtx, in.ID); err != nil {
50 0 : return err
51 0 : }
52 0 : remainingList, err := uc.ulRepo.ListByUserID(txCtx, in.UserID)
53 0 : if err != nil {
54 0 : return err
55 0 : }
56 0 : var newOrderIDs []uint64
57 0 : for _, con := range remainingList {
58 0 : newOrderIDs = append(newOrderIDs, con.ID)
59 0 : }
60 :
61 0 : if len(newOrderIDs) > 0 {
62 0 : if err := uc.ulRepo.UpdateOrder(txCtx, in.UserID, newOrderIDs); err != nil {
63 0 : return err
64 0 : }
65 : }
66 0 : return nil
67 0 : }); err != nil {
68 0 : return DeleteUserLicenseOutput{}, err
69 0 : }
70 0 : return DeleteUserLicenseOutput{}, nil
71 : }
|