Line data Source code
1 : // Package controller は 連絡先 に関するエンドポイントを提供します
2 : package controller
3 :
4 : import (
5 : "errors"
6 : "net/http"
7 : "strconv"
8 :
9 : "github.com/gin-gonic/gin"
10 : "github.com/go-playground/validator/v10"
11 :
12 : "resume/internal/adapter/http/dto/request"
13 : "resume/internal/adapter/http/handlerutil"
14 : "resume/internal/shared/apperr"
15 : "resume/internal/shared/ctx/auth"
16 : uccontact "resume/internal/usecase/contact"
17 : )
18 :
19 : // ContactHandler は 連絡先 に関するHTTPエンドポイントのハンドラです
20 : type ContactHandler struct {
21 : uc uccontact.Usecase
22 : }
23 :
24 : // NewContactHandler は 連絡先 ユースケースを注入した新しい ContactHandler を生成します
25 0 : func NewContactHandler(uc uccontact.Usecase) *ContactHandler {
26 0 : return &ContactHandler{
27 0 : uc: uc,
28 0 : }
29 0 : }
30 :
31 : // ListContact は 連絡先一覧 を取得します
32 0 : func (h *ContactHandler) ListContact(c *gin.Context) {
33 0 : _, ok := auth.From(c.Request.Context())
34 0 : if !ok {
35 0 : handlerutil.WriteError(
36 0 : c,
37 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
38 0 : )
39 0 : return
40 0 : }
41 0 : userID, ok := auth.UserID(c)
42 0 : if !ok || userID == 0 {
43 0 : handlerutil.WriteError(
44 0 : c,
45 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
46 0 : )
47 0 : return
48 0 : }
49 0 : in := uccontact.ListUserContactInput{
50 0 : UserID: userID,
51 0 : }
52 0 : out, err := h.uc.ListUserContact(c.Request.Context(), in)
53 0 : if err != nil {
54 0 : handlerutil.WriteError(c, err)
55 0 : return
56 0 : }
57 0 : c.JSON(200, out)
58 : }
59 :
60 : // AddContact は 連絡先情報 を作成します
61 0 : func (h *ContactHandler) AddContact(c *gin.Context) {
62 0 : _, ok := auth.From(c.Request.Context())
63 0 : if !ok {
64 0 : handlerutil.WriteError(
65 0 : c,
66 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
67 0 : )
68 0 : return
69 0 : }
70 0 : var req request.AddUserContactRequest
71 0 : if err := c.ShouldBindJSON(&req); err != nil {
72 0 : var verrs validator.ValidationErrors
73 0 : if errors.As(err, &verrs) {
74 0 : env := handlerutil.BuildValidationEnvelope(c, verrs, "domain.userContact")
75 0 : handlerutil.WriteValidationError(c, env)
76 0 : return
77 0 : }
78 0 : handlerutil.WriteError(c, err)
79 0 : return
80 : }
81 0 : userID, ok := auth.UserID(c)
82 0 : if !ok || userID == 0 {
83 0 : handlerutil.WriteError(
84 0 : c,
85 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
86 0 : )
87 0 : return
88 0 : }
89 0 : in := uccontact.CreateUserContactInput{
90 0 : UserID: userID,
91 0 : RelationshipTypeID: req.RelationshipTypeID,
92 0 : DisplayName: req.DisplayName,
93 0 : Email: req.Email,
94 0 : Phone: req.Phone,
95 0 : Note: req.Note,
96 0 : }
97 0 : out, err := h.uc.CreateUserContact(c.Request.Context(), in)
98 0 : if err != nil {
99 0 : handlerutil.WriteError(c, err)
100 0 : return
101 0 : }
102 0 : c.JSON(200, out)
103 : }
104 :
105 : // EditContact は 認証済みユーザーが 連絡先 を更新する
106 0 : func (h *ContactHandler) EditContact(c *gin.Context) {
107 0 : _, ok := auth.From(c.Request.Context())
108 0 : if !ok {
109 0 : handlerutil.WriteError(
110 0 : c,
111 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
112 0 : )
113 0 : return
114 0 : }
115 0 : var req request.EditUserContactRequest
116 0 : if err := c.ShouldBindJSON(&req); err != nil {
117 0 : var verrs validator.ValidationErrors
118 0 : if errors.As(err, &verrs) {
119 0 : env := handlerutil.BuildValidationEnvelope(c, verrs, "domain.userContact")
120 0 : handlerutil.WriteValidationError(c, env)
121 0 : return
122 0 : }
123 0 : handlerutil.WriteError(c, err)
124 0 : return
125 : }
126 0 : userID, ok := auth.UserID(c)
127 0 : if !ok || userID == 0 {
128 0 : handlerutil.WriteError(
129 0 : c,
130 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
131 0 : )
132 0 : return
133 0 : }
134 0 : contactIDStr := c.Param("contact_id")
135 0 : contactID, err := strconv.ParseUint(contactIDStr, 10, 64)
136 0 : if err != nil {
137 0 : c.JSON(http.StatusBadRequest, gin.H{
138 0 : "error": "invalid contact_id",
139 0 : })
140 0 : return
141 0 : }
142 0 : in := uccontact.UpdateUserContactInput{
143 0 : ID: contactID,
144 0 : UserID: userID,
145 0 : RelationshipTypeID: req.RelationshipTypeID,
146 0 : DisplayName: req.DisplayName,
147 0 : Email: req.Email,
148 0 : Phone: req.Phone,
149 0 : Note: req.Note,
150 0 : }
151 0 : out, err := h.uc.UpdateUserContact(c.Request.Context(), in)
152 0 : if err != nil {
153 0 : handlerutil.WriteError(c, err)
154 0 : return
155 0 : }
156 0 : c.JSON(200, out)
157 : }
158 :
159 : // DeleteContact は 認証済みユーザーが 連絡先 を削除する
160 : func (h *ContactHandler) DeleteContact(
161 : c *gin.Context,
162 0 : ) {
163 0 : _, ok := auth.From(c.Request.Context())
164 0 : if !ok {
165 0 : handlerutil.WriteError(
166 0 : c,
167 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
168 0 : )
169 0 : return
170 0 : }
171 0 : userID, ok := auth.UserID(c)
172 0 : if !ok || userID == 0 {
173 0 : handlerutil.WriteError(
174 0 : c,
175 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
176 0 : )
177 0 : return
178 0 : }
179 0 : contactIDStr := c.Param("contact_id")
180 0 : contactID, err := strconv.ParseUint(contactIDStr, 10, 64)
181 0 : if err != nil {
182 0 : c.JSON(http.StatusBadRequest, gin.H{
183 0 : "error": "invalid contact_id",
184 0 : })
185 0 : return
186 0 : }
187 0 : in := uccontact.DeleteUserContactInput{
188 0 : ID: contactID,
189 0 : UserID: userID,
190 0 : }
191 0 : _, err = h.uc.DeleteUserContact(c.Request.Context(), in)
192 0 : if err != nil {
193 0 : handlerutil.WriteError(c, err)
194 0 : return
195 0 : }
196 0 : c.JSON(http.StatusNoContent, nil)
197 : }
198 :
199 : // ReorderContact は 認証済みユーザーが 連絡先 の 並び替え をする
200 0 : func (h *ContactHandler) ReorderContact(c *gin.Context) {
201 0 : _, ok := auth.From(c.Request.Context())
202 0 : if !ok {
203 0 : handlerutil.WriteError(
204 0 : c,
205 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
206 0 : )
207 0 : }
208 0 : var req request.ReorderUserContactRequest
209 0 : if err := c.ShouldBindJSON(&req); err != nil {
210 0 : var verrs validator.ValidationErrors
211 0 : if errors.As(err, &verrs) {
212 0 : env := handlerutil.BuildValidationEnvelope(c, verrs, "domain.userContact")
213 0 : handlerutil.WriteValidationError(c, env)
214 0 : return
215 0 : }
216 0 : handlerutil.WriteError(c, err)
217 0 : return
218 : }
219 0 : userID, ok := auth.UserID(c)
220 0 : if !ok || userID == 0 {
221 0 : handlerutil.WriteError(
222 0 : c,
223 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
224 0 : )
225 0 : return
226 0 : }
227 0 : in := uccontact.ReorderUserContactInput{
228 0 : UserID: userID,
229 0 : IDs: req.IDs,
230 0 : }
231 0 : _, err := h.uc.ReorderUserContact(c.Request.Context(), in)
232 0 : if err != nil {
233 0 : handlerutil.WriteError(c, err)
234 0 : return
235 0 : }
236 0 : c.Status(http.StatusNoContent)
237 : }
238 :
239 : // ExistsContact は 認証済みユーザーが 連絡先 を 確認する
240 0 : func (h *ContactHandler) ExistsContact(c *gin.Context) {
241 0 : userID, ok := auth.UserID(c)
242 0 : if !ok || userID == 0 {
243 0 : handlerutil.WriteError(c, apperr.New(apperr.CodeUnauthorized, "unauthorized", nil))
244 0 : return
245 0 : }
246 0 : in := uccontact.ExistsUserContactInput{
247 0 : UserID: userID,
248 0 : }
249 0 : out, err := h.uc.ExistsUserContact(c.Request.Context(), in)
250 0 : if err != nil {
251 0 : handlerutil.WriteError(c, err)
252 0 : return
253 0 : }
254 0 : c.JSON(http.StatusOK, out)
255 : }
|