Line data Source code
1 : package service
2 :
3 : import (
4 : "context"
5 : "fmt"
6 :
7 : "resume/internal/domain/entity"
8 : "resume/internal/domain/repository"
9 : )
10 :
11 : // UserContactService は ユーザーの連絡先に関するドメインサービスです。
12 : type UserContactService struct {
13 : repo repository.UserContactRepository
14 : }
15 :
16 : // NewUserContactService は UserContactService を生成します。
17 0 : func NewUserContactService(repo repository.UserContactRepository) *UserContactService {
18 0 : return &UserContactService{repo: repo}
19 0 : }
20 :
21 : // IsUniqueRelationshipType は 一意であるべき続柄かどうかを判定します。
22 0 : func (s *UserContactService) IsUniqueRelationshipType(relationshipTypeID uint64) bool {
23 0 : switch relationshipTypeID {
24 : case entity.RelationshipTypeSelfID,
25 : entity.RelationshipTypeHusbandID,
26 : entity.RelationshipTypeWifeID,
27 : entity.RelationshipTypeFatherID,
28 : entity.RelationshipTypeMotherID,
29 : entity.RelationshipTypeFatherInLawID,
30 : entity.RelationshipTypeMotherInLawID,
31 : entity.RelationshipTypeStepFatherID,
32 : entity.RelationshipTypeStepMotherID,
33 : entity.RelationshipTypeAdoptiveFatherID,
34 0 : entity.RelationshipTypeAdoptiveMotherID:
35 0 : return true
36 0 : default:
37 0 : return false
38 : }
39 : }
40 :
41 : // CheckDuplicateRelationship は 続柄の重複をチェックします。
42 : // 重複がある場合はエラーを返します。
43 0 : func (s *UserContactService) CheckDuplicateRelationship(ctx context.Context, uc *entity.UserContact) error {
44 0 : // 一意制約の対象外の続柄(子など)はチェック不要
45 0 : if !s.IsUniqueRelationshipType(uc.RelationshipTypeID) {
46 0 : return nil
47 0 : }
48 :
49 0 : existing, err := s.repo.FindByUserIDAndRelationshipTypeID(ctx, uc.UserID, uc.RelationshipTypeID)
50 0 : if err != nil {
51 0 : return err
52 0 : }
53 :
54 : // 既に存在し、かつ自分自身(更新時)でない場合は重複エラー
55 0 : if existing != nil && existing.ID != uc.ID {
56 0 : return fmt.Errorf("relationship_type_id %d already exists for user %d", uc.RelationshipTypeID, uc.UserID)
57 0 : }
58 :
59 0 : return nil
60 : }
|