Line data Source code
1 : // Package contact は 連絡先 に関するユースケース(アプリケーションロジック)を提供します。
2 : // ドメイン層のエンティティやリポジトリを操作し、アプリケーション全体で再利用可能なビジネスフローを実装します。
3 : package contact
4 :
5 : import (
6 : "context"
7 :
8 : "resume/internal/domain/entity"
9 : "resume/internal/shared/apperr"
10 : "resume/internal/shared/valerr"
11 : )
12 :
13 : // CreateUserContact は 連絡先情報 を 作成 します
14 : func (uc *Interactor) CreateUserContact(
15 : ctx context.Context,
16 : in CreateUserContactInput,
17 0 : ) (CreateUserContactOutput, error) {
18 0 : contact := &entity.UserContact{
19 0 : UserID: in.UserID,
20 0 : RelationshipTypeID: in.RelationshipTypeID,
21 0 : DisplayName: in.DisplayName,
22 0 : Email: in.Email,
23 0 : Phone: in.Phone,
24 0 : Note: in.Note,
25 0 : }
26 0 : var saved uint64
27 0 : err := uc.tx.Do(ctx, func(txCtx context.Context) error {
28 0 :
29 0 : // NOTE: 一意制約のある 続柄ID の整合確認
30 0 : err := uc.userContactService.CheckDuplicateRelationship(txCtx, contact)
31 0 : if err != nil {
32 0 : ve := valerr.New()
33 0 : ve.Add(
34 0 : "domain.userContact.relationshipTypeId",
35 0 : "validation.rules.unique",
36 0 : map[string]any{
37 0 : "field": "domain.userContact.relationshipTypeId.label",
38 0 : "value": in.RelationshipTypeID,
39 0 : },
40 0 : )
41 0 : return apperr.New(
42 0 : apperr.CodeUnprocessable,
43 0 : "The request contains semantically invalid data.",
44 0 : ve.ToDetails(),
45 0 : )
46 0 : }
47 :
48 0 : count, err := uc.ucRepo.CountByUserID(txCtx, in.UserID)
49 0 : if err != nil {
50 0 : return err
51 0 : }
52 0 : contact.SortOrder = count
53 0 :
54 0 : res, err := uc.ucRepo.Create(txCtx, contact)
55 0 : if err != nil {
56 0 : return err
57 0 : }
58 0 : saved = res
59 0 : return nil
60 : })
61 0 : if err != nil {
62 0 : return CreateUserContactOutput{}, err
63 0 : }
64 0 : return CreateUserContactOutput{
65 0 : ID: saved,
66 0 : }, nil
67 : }
|