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 : "resume/internal/shared/util"
17 : uclicense "resume/internal/usecase/license"
18 : )
19 :
20 : // LicenseHandler は 資格・免許 を提供するHTTPエンドポイントのハンドラです
21 : type LicenseHandler struct {
22 : uc uclicense.Usecase
23 : }
24 :
25 : // NewLicenseHandler は 資格・免許ユースケースを注入した新しい LicenseHandler を生成します
26 : func NewLicenseHandler(
27 : uc uclicense.Usecase,
28 0 : ) *LicenseHandler {
29 0 : return &LicenseHandler{
30 0 : uc: uc,
31 0 : }
32 0 : }
33 :
34 : // AddLicense は 資格・免許 を 登録 します
35 0 : func (h *LicenseHandler) AddLicense(c *gin.Context) {
36 0 :
37 0 : _, ok := auth.From(c.Request.Context())
38 0 : if !ok {
39 0 : handlerutil.WriteError(
40 0 : c,
41 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
42 0 : )
43 0 : return
44 0 : }
45 :
46 0 : var req request.AddUserLicenseRequest
47 0 : if err := c.ShouldBindJSON(&req); err != nil {
48 0 : var verrs validator.ValidationErrors
49 0 : if errors.As(err, &verrs) {
50 0 : env := handlerutil.BuildValidationEnvelope(c, verrs, "domain.userLicense")
51 0 : handlerutil.WriteValidationError(c, env)
52 0 : return
53 0 : }
54 0 : handlerutil.WriteError(c, err)
55 0 : return
56 : }
57 :
58 0 : userID, ok := auth.UserID(c)
59 0 : if !ok || userID == 0 {
60 0 : handlerutil.WriteError(
61 0 : c,
62 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
63 0 : )
64 0 : return
65 0 : }
66 :
67 0 : eventDate, err := util.ParseYearMonthVal(req.EventDate)
68 0 : if err != nil {
69 0 : handlerutil.WriteError(c, apperr.New(apperr.CodeBadRequest, "invalid event date", nil))
70 0 : }
71 :
72 0 : in := uclicense.CreateUserLicenseInput{
73 0 : UserID: userID,
74 0 : Name: req.Name,
75 0 : EventDate: eventDate,
76 0 : }
77 0 : out, err := h.uc.CreateUserLicense(c.Request.Context(), in)
78 0 : if err != nil {
79 0 : handlerutil.WriteError(c, err)
80 0 : return
81 0 : }
82 0 : c.JSON(http.StatusCreated, out)
83 : }
84 :
85 : // EditLicense は 資格・免許 を更新します
86 0 : func (h *LicenseHandler) EditLicense(c *gin.Context) {
87 0 : _, ok := auth.From(c.Request.Context())
88 0 : if !ok {
89 0 : handlerutil.WriteError(
90 0 : c,
91 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
92 0 : )
93 0 : return
94 0 : }
95 0 : var req request.EditUserLicenseRequest
96 0 : if err := c.ShouldBindJSON(&req); err != nil {
97 0 : var verrs validator.ValidationErrors
98 0 : if errors.As(err, &verrs) {
99 0 : env := handlerutil.BuildValidationEnvelope(c, verrs, "domain.userLicense")
100 0 : handlerutil.WriteValidationError(c, env)
101 0 : return
102 0 : }
103 0 : handlerutil.WriteError(c, err)
104 0 : return
105 : }
106 0 : userID, ok := auth.UserID(c)
107 0 : if !ok || userID == 0 {
108 0 : handlerutil.WriteError(
109 0 : c,
110 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
111 0 : )
112 0 : return
113 0 : }
114 0 : licenseIDStr := c.Param("license_id")
115 0 : licenseID, err := strconv.ParseUint(licenseIDStr, 10, 64)
116 0 : if err != nil {
117 0 : c.JSON(http.StatusBadRequest, gin.H{
118 0 : "error": "invalid license_id",
119 0 : })
120 0 : return
121 0 : }
122 0 : eventDate, err := util.ParseYearMonthVal(req.EventDate)
123 0 : if err != nil {
124 0 : handlerutil.WriteError(c, apperr.New(apperr.CodeBadRequest, "invalid event date", nil))
125 0 : }
126 0 : in := uclicense.UpdateUserLicenseInput{
127 0 : UserID: userID,
128 0 : ID: licenseID,
129 0 : Name: req.Name,
130 0 : EventDate: eventDate,
131 0 : }
132 0 : out, err := h.uc.UpdateUserLicense(c.Request.Context(), in)
133 0 : if err != nil {
134 0 : handlerutil.WriteError(c, err)
135 0 : return
136 0 : }
137 0 : c.JSON(http.StatusOK, out)
138 : }
139 :
140 : // DeleteLicense は 認証済みユーザーが 資格・免許 を削除する
141 0 : func (h *LicenseHandler) DeleteLicense(c *gin.Context) {
142 0 : _, ok := auth.From(c.Request.Context())
143 0 : if !ok {
144 0 : handlerutil.WriteError(
145 0 : c,
146 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
147 0 : )
148 0 : return
149 0 : }
150 0 : userID, ok := auth.UserID(c)
151 0 : if !ok || userID == 0 {
152 0 : handlerutil.WriteError(
153 0 : c,
154 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
155 0 : )
156 0 : return
157 0 : }
158 0 : licenseIDStr := c.Param("license_id")
159 0 : licenseID, err := strconv.ParseUint(licenseIDStr, 10, 64)
160 0 : if err != nil {
161 0 : c.JSON(http.StatusBadRequest, gin.H{
162 0 : "error": "invalid license_id",
163 0 : })
164 0 : return
165 0 : }
166 0 : in := uclicense.DeleteUserLicenseInput{
167 0 : UserID: userID,
168 0 : ID: licenseID,
169 0 : }
170 0 : _, err = h.uc.DeleteUserLicense(c.Request.Context(), in)
171 0 : if err != nil {
172 0 : handlerutil.WriteError(c, err)
173 0 : return
174 0 : }
175 0 : c.JSON(http.StatusNoContent, nil)
176 : }
177 :
178 : // DetailLicense は 認証済みユーザーが 資格・免許 を1件取得する
179 0 : func (h *LicenseHandler) DetailLicense(c *gin.Context) {
180 0 : _, ok := auth.From(c.Request.Context())
181 0 : if !ok {
182 0 : handlerutil.WriteError(
183 0 : c,
184 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
185 0 : )
186 0 : return
187 0 : }
188 0 : userID, ok := auth.UserID(c)
189 0 : if !ok || userID == 0 {
190 0 : handlerutil.WriteError(
191 0 : c,
192 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
193 0 : )
194 0 : return
195 0 : }
196 0 : licenseIDStr := c.Param("license_id")
197 0 : licenseID, err := strconv.ParseUint(licenseIDStr, 10, 64)
198 0 : if err != nil {
199 0 : c.JSON(http.StatusBadRequest, gin.H{
200 0 : "error": "invalid license_id",
201 0 : })
202 0 : return
203 0 : }
204 0 : in := uclicense.DetailUserLicenseInput{
205 0 : UserID: userID,
206 0 : ID: licenseID,
207 0 : }
208 0 : out, err := h.uc.DetailUserLicense(c.Request.Context(), in)
209 0 : if err != nil {
210 0 : handlerutil.WriteError(c, err)
211 0 : return
212 0 : }
213 0 : c.JSON(http.StatusOK, out)
214 : }
215 :
216 : // ListLicense は 認証済みユーザーが 資格・免許 を取得する
217 0 : func (h *LicenseHandler) ListLicense(c *gin.Context) {
218 0 : _, ok := auth.From(c.Request.Context())
219 0 : if !ok {
220 0 : handlerutil.WriteError(
221 0 : c,
222 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
223 0 : )
224 0 : return
225 0 : }
226 0 : userID, ok := auth.UserID(c)
227 0 : if !ok || userID == 0 {
228 0 : handlerutil.WriteError(
229 0 : c,
230 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
231 0 : )
232 0 : return
233 0 : }
234 0 : in := uclicense.ListUserLicenseInput{
235 0 : UserID: userID,
236 0 : }
237 0 : out, err := h.uc.ListUserLicense(c.Request.Context(), in)
238 0 : if err != nil {
239 0 : handlerutil.WriteError(c, err)
240 0 : return
241 0 : }
242 0 : c.JSON(http.StatusOK, out)
243 : }
244 :
245 : // ExistsLicense は 認証済みユーザーが 資格・免許 を取得する
246 0 : func (h *LicenseHandler) ExistsLicense(c *gin.Context) {
247 0 : _, ok := auth.From(c.Request.Context())
248 0 : if !ok {
249 0 : handlerutil.WriteError(
250 0 : c,
251 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
252 0 : )
253 0 : return
254 0 : }
255 0 : userID, ok := auth.UserID(c)
256 0 : if !ok || userID == 0 {
257 0 : handlerutil.WriteError(
258 0 : c,
259 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
260 0 : )
261 0 : return
262 0 : }
263 0 : in := uclicense.ExistsUserLicenseInput{
264 0 : UserID: userID,
265 0 : }
266 0 : out, err := h.uc.ExistsUserLicense(c.Request.Context(), in)
267 0 : if err != nil {
268 0 : handlerutil.WriteError(c, err)
269 0 : return
270 0 : }
271 0 : c.JSON(http.StatusOK, out)
272 : }
273 :
274 : // ReorderLicense は 認証済みユーザーが 資格・免許 を再順序化する
275 0 : func (h *LicenseHandler) ReorderLicense(c *gin.Context) {
276 0 : _, ok := auth.From(c.Request.Context())
277 0 : if !ok {
278 0 : handlerutil.WriteError(
279 0 : c,
280 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
281 0 : )
282 0 : }
283 0 : var req request.ReorderUserLicenseRequest
284 0 : if err := c.ShouldBindJSON(&req); err != nil {
285 0 : var verrs validator.ValidationErrors
286 0 : if errors.As(err, &verrs) {
287 0 : env := handlerutil.BuildValidationEnvelope(c, verrs, "domain.userLicense")
288 0 : handlerutil.WriteValidationError(c, env)
289 0 : return
290 0 : }
291 0 : handlerutil.WriteError(c, err)
292 0 : return
293 : }
294 0 : userID, ok := auth.UserID(c)
295 0 : if !ok || userID == 0 {
296 0 : handlerutil.WriteError(
297 0 : c,
298 0 : apperr.New(apperr.CodeUnauthorized, "unauthorized", nil),
299 0 : )
300 0 : return
301 0 : }
302 0 : in := uclicense.ReorderUserLicenseInput{
303 0 : UserID: userID,
304 0 : IDs: req.IDs,
305 0 : }
306 0 : _, err := h.uc.ReorderUserLicense(c.Request.Context(), in)
307 0 : if err != nil {
308 0 : handlerutil.WriteError(c, err)
309 0 : return
310 0 : }
311 0 : c.Status(http.StatusNoContent)
312 : }
|