forked from GP3-group1/ALTA-Airbnb-BE
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcover.html
957 lines (807 loc) · 39.4 KB
/
cover.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>service: Go Coverage Report</title>
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">alta-airbnb-be/features/images/service/image.go (92.5%)</option>
<option value="file1">alta-airbnb-be/features/reservations/delivery/adapter.go (100.0%)</option>
<option value="file2">alta-airbnb-be/features/reservations/delivery/handler.go (100.0%)</option>
<option value="file3">alta-airbnb-be/features/reservations/service/reservation.go (95.5%)</option>
<option value="file4">alta-airbnb-be/features/rooms/service/room.go (100.0%)</option>
<option value="file5">alta-airbnb-be/features/rooms/service/validate.go (100.0%)</option>
<option value="file6">alta-airbnb-be/features/users/delivery/adapter.go (100.0%)</option>
<option value="file7">alta-airbnb-be/features/users/delivery/handler.go (100.0%)</option>
<option value="file8">alta-airbnb-be/features/users/service/user.go (95.3%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">not covered</span>
<span class="cov8">covered</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" style="display: none">package service
import (
"alta-airbnb-be/features/images"
"alta-airbnb-be/features/rooms"
"alta-airbnb-be/utils/consts"
"errors"
"github.com/go-playground/validator/v10"
)
type ImageService struct {
imageData images.ImageData_
roomData rooms.RoomData_
validate *validator.Validate
}
func New(imageData images.ImageData_, roomData rooms.RoomData_) images.ImageService_ <span class="cov8" title="1">{
return &ImageService{
imageData: imageData,
roomData: roomData,
validate: validator.New(),
}
}</span>
func (imageService *ImageService) checkPermission(userId uint, imageEntity *images.ImageEntity) error <span class="cov8" title="1">{
roomEntity := rooms.RoomEntity{UserID: userId}
roomEntities, err := imageService.roomData.SelectRoomsByUserId(&roomEntity)
if err != nil </span><span class="cov8" title="1">{
return err
}</span>
<span class="cov8" title="1">if len(roomEntities) != 0 </span><span class="cov8" title="1">{
for _, val := range roomEntities </span><span class="cov8" title="1">{
if val.ID == imageEntity.RoomID </span><span class="cov8" title="1">{
return nil
}</span>
}
}
<span class="cov8" title="1">return errors.New(consts.SERVER_ForbiddenRequest)</span>
}
func (imageService *ImageService) CreateImage(userId uint, imageEntity *images.ImageEntity) (*images.ImageEntity, error) <span class="cov8" title="1">{
err := imageService.validate.Struct(imageEntity)
if err != nil </span><span class="cov8" title="1">{
return nil, errors.New(consts.IMAGE_InvalidInput)
}</span>
<span class="cov8" title="1">err = imageService.checkPermission(userId, imageEntity)
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
<span class="cov8" title="1">imageEntity, err = imageService.imageData.InsertImage(imageEntity)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="1">return imageEntity, nil</span>
}
func (imageService *ImageService) ChangeImage(userId uint, imageEntity *images.ImageEntity) (*images.ImageEntity, error) <span class="cov8" title="1">{
err := imageService.validate.Struct(imageEntity)
if err != nil </span><span class="cov8" title="1">{
return nil, errors.New(consts.IMAGE_InvalidInput)
}</span>
<span class="cov8" title="1">err = imageService.checkPermission(userId, imageEntity)
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
<span class="cov8" title="1">imageEntity, err = imageService.imageData.UpdateImage(imageEntity)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov8" title="1">return imageEntity, nil</span>
}
func (imageService *ImageService) RemoveImage(userId uint, imageEntity *images.ImageEntity) error <span class="cov8" title="1">{
err := imageService.validate.Struct(imageEntity)
if err != nil </span><span class="cov8" title="1">{
return errors.New(consts.IMAGE_InvalidInput)
}</span>
<span class="cov8" title="1">err = imageService.checkPermission(userId, imageEntity)
if err != nil </span><span class="cov8" title="1">{
return err
}</span>
<span class="cov8" title="1">err = imageService.imageData.DeleteImage(imageEntity)
if err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov8" title="1">return nil</span>
}
</pre>
<pre class="file" id="file1" style="display: none">package delivery
import (
"alta-airbnb-be/features/reservations"
"errors"
"time"
)
func insertToEntity(reservationInsert reservations.ReservationInsert) (reservations.ReservationEntity, error) <span class="cov8" title="1">{
CheckInDate, err := time.Parse("2006-01-02", reservationInsert.CheckInDate)
if err != nil </span><span class="cov8" title="1">{
return reservations.ReservationEntity{}, errors.New("invalid check in date format must YYYY-MM-DD")
}</span>
<span class="cov8" title="1">CheckOutDate, err := time.Parse("2006-01-02", reservationInsert.CheckOutDate)
if err != nil </span><span class="cov8" title="1">{
return reservations.ReservationEntity{}, errors.New("invalid check out date format must YYYY-MM-DD")
}</span>
<span class="cov8" title="1">return reservations.ReservationEntity{
CheckInDate: CheckInDate,
CheckOutDate: CheckOutDate,
}, nil</span>
}
func entityToResponse(reservationEntity reservations.ReservationEntity) reservations.ReservationResponse <span class="cov8" title="1">{
CheckInDate := reservationEntity.CheckInDate.Format("2006-01-02")
CheckOutDate := reservationEntity.CheckOutDate.Format("2006-01-02")
return reservations.ReservationResponse{
ID: reservationEntity.ID,
RoomID: reservationEntity.RoomID,
RoomName: reservationEntity.Room.Name,
CheckInDate: CheckInDate,
CheckOutDate: CheckOutDate,
Price: float64(reservationEntity.Room.Price),
TotalNight: reservationEntity.TotalNight,
TotalPrice: reservationEntity.TotalPrice,
}
}</span>
func entityToResponseList(reservationEntity []reservations.ReservationEntity) []reservations.ReservationResponse <span class="cov8" title="1">{
var dataResponse []reservations.ReservationResponse
for _, v := range reservationEntity </span><span class="cov8" title="1">{
dataResponse = append(dataResponse, entityToResponse(v))
}</span>
<span class="cov8" title="1">return dataResponse</span>
}
</pre>
<pre class="file" id="file2" style="display: none">package delivery
import (
"alta-airbnb-be/features/reservations"
"alta-airbnb-be/middlewares"
"alta-airbnb-be/utils/consts"
"alta-airbnb-be/utils/helpers"
"net/http"
"strconv"
"github.com/labstack/echo/v4"
)
type ReservationHandler struct {
reservationService reservations.ReservationService_
}
// AddReservation implements reservations.ReservationDeliveryInterface_
func (reservationHandler *ReservationHandler) AddReservation(c echo.Context) error <span class="cov8" title="1">{
userID := middlewares.ExtractTokenUserId(c)
idParam, errParam := helpers.ExtractIDParam(c)
if errParam != nil </span><span class="cov8" title="1">{
return c.JSON(http.StatusBadRequest, helpers.Response(errParam.Error()))
}</span>
<span class="cov8" title="1">input := reservations.ReservationInsert{}
errBind := c.Bind(&input)
if errBind != nil </span><span class="cov8" title="1">{
return c.JSON(http.StatusBadRequest, helpers.Response(consts.RESERVATION_ErrorBindReservationData))
}</span>
<span class="cov8" title="1">reservationEntity, errMapping := insertToEntity(input)
if errMapping != nil </span><span class="cov8" title="1">{
return c.JSON(http.StatusBadRequest, helpers.Response(errMapping.Error()))
}</span>
<span class="cov8" title="1">midTransResponse, errInsert := reservationHandler.reservationService.Create(userID, uint(idParam), reservationEntity)
if errInsert != nil </span><span class="cov8" title="1">{
return c.JSON(helpers.ErrorResponse(errInsert))
}</span>
<span class="cov8" title="1">dataResponse := map[string]any{
"token": midTransResponse.Token,
"redirect_url": midTransResponse.RedirectUrl,
}
return c.JSON(http.StatusCreated, helpers.ResponseWithData(consts.RESERVATION_InsertSuccess, dataResponse))</span>
}
// CheckReservation implements reservations.ReservationDeliveryInterface_
func (reservationHandler *ReservationHandler) CheckReservation(c echo.Context) error <span class="cov8" title="1">{
idParam, errParam := helpers.ExtractIDParam(c)
if errParam != nil </span><span class="cov8" title="1">{
return c.JSON(http.StatusBadRequest, helpers.Response(errParam.Error()))
}</span>
<span class="cov8" title="1">input := reservations.ReservationInsert{}
errBind := c.Bind(&input)
if errBind != nil </span><span class="cov8" title="1">{
return c.JSON(http.StatusBadRequest, helpers.Response(consts.RESERVATION_ErrorBindReservationData))
}</span>
<span class="cov8" title="1">inputReservationEntity, errMapping := insertToEntity(input)
if errMapping != nil </span><span class="cov8" title="1">{
return c.JSON(http.StatusBadRequest, helpers.Response(errMapping.Error()))
}</span>
<span class="cov8" title="1">row, errSelect := reservationHandler.reservationService.CheckReservation(inputReservationEntity, idParam)
if errSelect != nil </span><span class="cov8" title="1">{
return c.JSON(helpers.ErrorResponse(errSelect))
}</span>
<span class="cov8" title="1">if row == 0 </span><span class="cov8" title="1">{
return c.JSON(http.StatusOK, helpers.Response(consts.RESERVATION_RoomAvailable))
}</span>
<span class="cov8" title="1">return c.JSON(http.StatusBadRequest, helpers.Response(consts.RESERVATION_RoomNotAvailable))</span>
}
// GetAllReservation implements reservations.ReservationDeliveryInterface_
func (reservationHandler *ReservationHandler) GetAllReservation(c echo.Context) error <span class="cov8" title="1">{
userID := middlewares.ExtractTokenUserId(c)
var page int = 1
pageParam := c.QueryParam("page")
if pageParam != "" </span><span class="cov8" title="1">{
pageConv, errConv := strconv.Atoi(pageParam)
if errConv != nil </span><span class="cov8" title="1">{
return c.JSON(http.StatusBadRequest, helpers.Response(consts.ECHO_InvaildPageParam))
}</span> else<span class="cov8" title="1"> {
page = pageConv
}</span>
}
<span class="cov8" title="1">var limit int = 8
limitParam := c.QueryParam("limit")
if limitParam != "" </span><span class="cov8" title="1">{
limitConv, errConv := strconv.Atoi(limitParam)
if errConv != nil </span><span class="cov8" title="1">{
return c.JSON(http.StatusBadRequest, helpers.Response(consts.ECHO_InvaildLimitParam))
}</span> else<span class="cov8" title="1"> {
limit = limitConv
}</span>
}
<span class="cov8" title="1">reservationEntity, errSelect := reservationHandler.reservationService.GetAll(page, limit, userID)
if errSelect != nil </span><span class="cov8" title="1">{
return c.JSON(helpers.ErrorResponse(errSelect))
}</span>
<span class="cov8" title="1">return c.JSON(http.StatusOK, helpers.ResponseWithData("Success", entityToResponseList(reservationEntity)))</span>
}
func New(reservationService reservations.ReservationService_) reservations.ReservationDelivery_ <span class="cov8" title="1">{
return &ReservationHandler{
reservationService: reservationService,
}
}</span>
</pre>
<pre class="file" id="file3" style="display: none">package service
import (
"alta-airbnb-be/features/reservations"
"alta-airbnb-be/features/users"
"alta-airbnb-be/utils/consts"
"alta-airbnb-be/utils/helpers"
"errors"
"github.com/go-playground/validator/v10"
)
type reservationService struct {
reservationData reservations.ReservationData_
validate *validator.Validate
}
// CheckReservation implements reservations.ReservationService_
func (reservationService *reservationService) CheckReservation(input reservations.ReservationEntity, roomID uint) (int64, error) <span class="cov8" title="1">{
errValidate := reservationService.validate.StructExcept(input, "Room", "User")
if errValidate != nil </span><span class="cov8" title="1">{
return 0, errValidate
}</span>
// date validation: check in date must lower than check out date
<span class="cov8" title="1">diff := input.CheckOutDate.Sub(input.CheckInDate)
totalNight := int(diff.Hours() / 24)
if totalNight < 1 </span><span class="cov8" title="1">{
return 0, errors.New(consts.RESERVATION_InvalidInput)
}</span>
<span class="cov8" title="1">row, errSelect := reservationService.reservationData.CheckReservation(input, roomID)
if errSelect != nil </span><span class="cov8" title="1">{
return 0, errSelect
}</span>
<span class="cov8" title="1">return row, nil</span>
}
// GetAll implements reservations.ReservationServiceInterface_
func (reservationService *reservationService) GetAll(page, limit int, userID uint) ([]reservations.ReservationEntity, error) <span class="cov8" title="1">{
offset := (page - 1) * limit
reservationEntity, errSelect := reservationService.reservationData.SelectAll(limit, offset, userID)
if errSelect != nil </span><span class="cov8" title="1">{
return []reservations.ReservationEntity{}, errSelect
}</span>
<span class="cov8" title="1">return reservationEntity, nil</span>
}
// Create implements reservations.ReservationServiceInterface_
func (reservationService *reservationService) Create(userID, idParam uint, inputReservation reservations.ReservationEntity) (reservations.MidtransResponse, error) <span class="cov8" title="1">{
errValidate := reservationService.validate.StructExcept(inputReservation, "Room", "User")
if errValidate != nil </span><span class="cov8" title="1">{
return reservations.MidtransResponse{}, errValidate
}</span>
// input user id and room id
<span class="cov8" title="1">inputReservation.UserID = userID
inputReservation.RoomID = idParam
// count total night
diff := inputReservation.CheckOutDate.Sub(inputReservation.CheckInDate)
inputReservation.TotalNight = int(diff.Hours() / 24)
// input date validation
if inputReservation.TotalNight < 1 </span><span class="cov8" title="1">{
return reservations.MidtransResponse{}, errors.New(consts.RESERVATION_InvalidInput)
}</span>
// get room data by id
<span class="cov8" title="1">selectRoom, errSelectRoom := reservationService.reservationData.SelectRoom(idParam)
if errSelectRoom != nil </span><span class="cov8" title="1">{
return reservations.MidtransResponse{}, errSelectRoom
}</span>
// count total price
<span class="cov8" title="1">inputReservation.TotalPrice = float64(selectRoom.Room.Price) * float64(inputReservation.TotalNight)
// get user data by id
selectUser, errSelectUser := reservationService.reservationData.SelectUser(userID)
if errSelectUser != nil </span><span class="cov8" title="1">{
return reservations.MidtransResponse{}, errSelectUser
}</span>
// balance validation
<span class="cov8" title="1">if selectUser.User.Balance < inputReservation.TotalPrice </span><span class="cov8" title="1">{
return reservations.MidtransResponse{}, errors.New(consts.RESERVATION_InsertFailed)
}</span>
// decrease user balance
<span class="cov8" title="1">inputUser := users.UserEntity{}
inputUser.Balance = selectUser.User.Balance - inputReservation.TotalPrice
// insert to reservations and update user balance
errInsert := reservationService.reservationData.Insert(inputReservation, inputUser, userID)
if errInsert != nil </span><span class="cov0" title="0">{
return reservations.MidtransResponse{}, errInsert
}</span>
// midtrans helpers
<span class="cov8" title="1">midtransResponse, errSnap := helpers.RequestSnapMidtrans(selectUser, selectRoom, inputReservation)
if errSnap != nil </span><span class="cov8" title="1">{
return reservations.MidtransResponse{}, errSnap
}</span>
<span class="cov0" title="0">return midtransResponse, nil</span>
}
func New(reservationData reservations.ReservationData_) reservations.ReservationService_ <span class="cov8" title="1">{
return &reservationService{
reservationData: reservationData,
validate: validator.New(),
}
}</span>
</pre>
<pre class="file" id="file4" style="display: none">package service
import (
"alta-airbnb-be/features/reviews"
"alta-airbnb-be/features/rooms"
"alta-airbnb-be/utils/consts"
"alta-airbnb-be/utils/helpers"
"errors"
"net/url"
"github.com/go-playground/validator/v10"
)
type RoomService struct {
roomData rooms.RoomData_
validate *validator.Validate
}
func New(roomData rooms.RoomData_) rooms.RoomService_ <span class="cov8" title="1">{
return &RoomService{
roomData: roomData,
validate: validator.New(),
}
}</span>
func (roomService *RoomService) CreateRoom(roomEntity *rooms.RoomEntity) error <span class="cov8" title="1">{
err := roomService.validate.Struct(roomEntity)
if err != nil </span><span class="cov8" title="1">{
return errors.New(consts.ROOM_InvalidInput)
}</span>
<span class="cov8" title="1">err = roomService.roomData.InsertRoom(roomEntity)
if err != nil </span><span class="cov8" title="1">{
return err
}</span>
<span class="cov8" title="1">return nil</span>
}
func (roomService *RoomService) ChangeRoom(roomEntity *rooms.RoomEntity) error <span class="cov8" title="1">{
err := roomService.validate.Struct(roomEntity)
if err != nil </span><span class="cov8" title="1">{
return errors.New(consts.ROOM_InvalidInput)
}</span>
<span class="cov8" title="1">err = roomService.roomData.UpdateRoom(roomEntity)
if err != nil </span><span class="cov8" title="1">{
return err
}</span>
<span class="cov8" title="1">return nil</span>
}
func (roomService *RoomService) RemoveRoom(roomEntity *rooms.RoomEntity) error <span class="cov8" title="1">{
err := roomService.roomData.DeleteRoom(roomEntity)
if err != nil </span><span class="cov8" title="1">{
return err
}</span>
<span class="cov8" title="1">return nil</span>
}
func (roomService *RoomService) GetRooms(limit, offset int, queryParams url.Values) ([]*rooms.RoomEntity, error) <span class="cov8" title="1">{
extractedQueryParams := helpers.ExtractQueryParams(queryParams)
roomEntities, err := roomService.roomData.SelectRooms(limit, offset, extractedQueryParams)
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
<span class="cov8" title="1">return roomEntities, nil</span>
}
func (roomService *RoomService) GetRoomByRoomId(roomEntity *rooms.RoomEntity) (*rooms.RoomEntity, error) <span class="cov8" title="1">{
roomEntity, err := roomService.roomData.SelectRoomByRoomId(roomEntity)
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
<span class="cov8" title="1">return roomEntity, nil</span>
}
func (roomService *RoomService) GetRoomsByUserId(roomEntity *rooms.RoomEntity) ([]*rooms.RoomEntity, error) <span class="cov8" title="1">{
roomEntities, err := roomService.roomData.SelectRoomsByUserId(roomEntity)
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
<span class="cov8" title="1">return roomEntities, nil</span>
}
func (roomService *RoomService) CreateReview(reviewEntity *reviews.ReviewEntity) error <span class="cov8" title="1">{
err := Validate(roomService, reviewEntity)
if err != nil </span><span class="cov8" title="1">{
return err
}</span>
<span class="cov8" title="1">err = roomService.roomData.InsertReview(reviewEntity)
if err != nil </span><span class="cov8" title="1">{
return err
}</span>
<span class="cov8" title="1">return nil</span>
}
func (roomService *RoomService) GetReviewsByRoomId(reviewEntity *reviews.ReviewEntity) ([]*reviews.ReviewEntity, error) <span class="cov8" title="1">{
reviewEntities, err := roomService.roomData.SelectReviewsByRoomId(reviewEntity)
if err != nil </span><span class="cov8" title="1">{
return nil, err
}</span>
<span class="cov8" title="1">return reviewEntities, nil</span>
}
</pre>
<pre class="file" id="file5" style="display: none">package service
import (
"alta-airbnb-be/features/reviews"
"alta-airbnb-be/utils/consts"
"errors"
)
func Validate(roomService *RoomService, reviewEntity *reviews.ReviewEntity) error <span class="cov8" title="1">{
err := roomService.validate.Struct(reviewEntity)
if err != nil </span><span class="cov8" title="1">{
return errors.New(consts.REVIEW_InvalidInput)
}</span>
<span class="cov8" title="1">if reviewEntity.Rating > 5.0 </span><span class="cov8" title="1">{
return errors.New(consts.REVIEW_InvalidRatingInputRange)
}</span>
<span class="cov8" title="1">return nil</span>
}</pre>
<pre class="file" id="file6" style="display: none">package delivery
import "alta-airbnb-be/features/users"
func registerToEntity(userRegister users.UserRegister) users.UserEntity <span class="cov8" title="1">{
return users.UserEntity{
Name: userRegister.Name,
Email: userRegister.Email,
Password: userRegister.Password,
}
}</span>
func requestUpdateToEntity(userUpdate users.UserUpdate) users.UserEntity <span class="cov8" title="1">{
return users.UserEntity{
Name: userUpdate.Name,
Email: userUpdate.Email,
Sex: userUpdate.Sex,
Address: userUpdate.Address,
PhoneNumber: userUpdate.PhoneNumber,
Balance: userUpdate.Balance,
}
}</span>
func requestUpdatePasswordToEntity(UpdatePassword users.UserUpdatePassword) users.UserEntity <span class="cov8" title="1">{
return users.UserEntity{
Password: UpdatePassword.Password,
NewPassword: UpdatePassword.NewPassword,
}
}</span>
func entityToResponse(userEntity users.UserEntity) users.UserResponse <span class="cov8" title="1">{
return users.UserResponse{
ID: userEntity.ID,
Name: userEntity.Name,
Email: userEntity.Email,
Sex: userEntity.Sex,
Address: userEntity.Address,
PhoneNumber: userEntity.PhoneNumber,
Balance: userEntity.Balance,
}
}</span>
</pre>
<pre class="file" id="file7" style="display: none">package delivery
import (
"alta-airbnb-be/features/users"
"alta-airbnb-be/middlewares"
"alta-airbnb-be/utils/consts"
"alta-airbnb-be/utils/helpers"
"net/http"
"github.com/labstack/echo/v4"
)
type UserHandler struct {
userService users.UserService_
}
// UpdateBalance implements users.UserDelivery_
func (userHandler *UserHandler) UpdateBalance(c echo.Context) error <span class="cov8" title="1">{
userID := middlewares.ExtractTokenUserId(c)
userInput := users.UserUpdate{}
errBind := c.Bind(&userInput)
if errBind != nil </span><span class="cov8" title="1">{
return c.JSON(http.StatusBadRequest, helpers.Response(consts.USER_ErrorBindUserData))
}</span>
<span class="cov8" title="1">errUpdate := userHandler.userService.UpdateBalance(userID, requestUpdateToEntity(userInput))
if errUpdate != nil </span><span class="cov8" title="1">{
return c.JSON(helpers.ErrorResponse(errUpdate))
}</span>
<span class="cov8" title="1">return c.JSON(http.StatusOK, helpers.Response(consts.USER_SuccessUpdateBalance))</span>
}
// GetUserBalance implements users.UserDelivery_
func (userHandler *UserHandler) GetUserBalance(c echo.Context) error <span class="cov8" title="1">{
userID := middlewares.ExtractTokenUserId(c)
userEntity, errSelect := userHandler.userService.GetData(userID)
if errSelect != nil </span><span class="cov8" title="1">{
return c.JSON(helpers.ErrorResponse(errSelect))
}</span>
<span class="cov8" title="1">response := entityToResponse(userEntity)
dataResponse := map[string]any{
"id": response.ID,
"balance": response.Balance,
}
return c.JSON(http.StatusOK, helpers.ResponseWithData(consts.USER_SuccessReadBalance, dataResponse))</span>
}
// GetUserData implements users.UserDeliveryInterface_
func (userHandler *UserHandler) GetUserData(c echo.Context) error <span class="cov8" title="1">{
userID := middlewares.ExtractTokenUserId(c)
userEntity, errSelect := userHandler.userService.GetData(userID)
if errSelect != nil </span><span class="cov8" title="1">{
return c.JSON(helpers.ErrorResponse(errSelect))
}</span>
<span class="cov8" title="1">return c.JSON(http.StatusOK, helpers.ResponseWithData(consts.USER_SuccessReadUserData, entityToResponse(userEntity)))</span>
}
// Login implements users.UserDeliveryInterface_
func (userHandler *UserHandler) Login(c echo.Context) error <span class="cov8" title="1">{
loginInput := users.UserLogin{}
errBind := c.Bind(&loginInput)
if errBind != nil </span><span class="cov8" title="1">{
return c.JSON(http.StatusBadRequest, helpers.Response(consts.USER_ErrorBindUserData))
}</span>
<span class="cov8" title="1">userEntity, token, errLogin := userHandler.userService.Login(loginInput.Email, loginInput.Password)
if errLogin != nil </span><span class="cov8" title="1">{
return c.JSON(helpers.ErrorResponse(errLogin))
}</span>
<span class="cov8" title="1">dataResponse := map[string]any{
"id": userEntity.ID,
"name": userEntity.Name,
"token": token,
}
return c.JSON(http.StatusOK, helpers.ResponseWithData(consts.USER_LoginSuccess, dataResponse))</span>
}
// Register implements users.UserDeliveryInterface_
func (userHandler *UserHandler) Register(c echo.Context) error <span class="cov8" title="1">{
userInput := users.UserRegister{}
errBind := c.Bind(&userInput)
if errBind != nil </span><span class="cov8" title="1">{
return c.JSON(http.StatusBadRequest, helpers.Response(consts.USER_ErrorBindUserData))
}</span>
<span class="cov8" title="1">errInsert := userHandler.userService.Create(registerToEntity(userInput))
if errInsert != nil </span><span class="cov8" title="1">{
return c.JSON(helpers.ErrorResponse(errInsert))
}</span>
<span class="cov8" title="1">return c.JSON(http.StatusCreated, helpers.Response(consts.USER_RegisterSuccess))</span>
}
// RemoveAccount implements users.UserDeliveryInterface_
func (userHandler *UserHandler) RemoveAccount(c echo.Context) error <span class="cov8" title="1">{
userID := middlewares.ExtractTokenUserId(c)
errDelete := userHandler.userService.Remove(userID)
if errDelete != nil </span><span class="cov8" title="1">{
return c.JSON(helpers.ErrorResponse(errDelete))
}</span>
<span class="cov8" title="1">return c.JSON(http.StatusOK, helpers.Response(consts.USER_SuccessDelete))</span>
}
// UpdateAccount implements users.UserDeliveryInterface_
func (userHandler *UserHandler) UpdateAccount(c echo.Context) error <span class="cov8" title="1">{
userID := middlewares.ExtractTokenUserId(c)
userInput := users.UserUpdate{}
errBind := c.Bind(&userInput)
if errBind != nil </span><span class="cov8" title="1">{
return c.JSON(http.StatusBadRequest, helpers.Response(consts.USER_ErrorBindUserData))
}</span>
<span class="cov8" title="1">errUpdate := userHandler.userService.ModifyData(userID, requestUpdateToEntity(userInput))
if errUpdate != nil </span><span class="cov8" title="1">{
return c.JSON(helpers.ErrorResponse(errUpdate))
}</span>
<span class="cov8" title="1">return c.JSON(http.StatusOK, helpers.Response(consts.USER_SuccessUpdateUserData))</span>
}
// UpdatePassword implements users.UserDeliveryInterface_
func (userHandler *UserHandler) UpdatePassword(c echo.Context) error <span class="cov8" title="1">{
userID := middlewares.ExtractTokenUserId(c)
input := users.UserUpdatePassword{}
errBind := c.Bind(&input)
if errBind != nil </span><span class="cov8" title="1">{
return c.JSON(http.StatusBadRequest, helpers.Response(consts.USER_ErrorBindUserData))
}</span>
<span class="cov8" title="1">errUpdatePassword := userHandler.userService.ModifyPassword(userID, requestUpdatePasswordToEntity(input))
if errUpdatePassword != nil </span><span class="cov8" title="1">{
return c.JSON(helpers.ErrorResponse(errUpdatePassword))
}</span>
<span class="cov8" title="1">return c.JSON(http.StatusOK, helpers.Response(consts.USER_SuccessUpdateUserData))</span>
}
func New(userService users.UserService_) users.UserDelivery_ <span class="cov8" title="1">{
return &UserHandler{
userService: userService,
}
}</span>
</pre>
<pre class="file" id="file8" style="display: none">package service
import (
"alta-airbnb-be/features/users"
"alta-airbnb-be/middlewares"
"alta-airbnb-be/utils/consts"
"alta-airbnb-be/utils/helpers"
"errors"
"github.com/go-playground/validator/v10"
)
type userService struct {
userData users.UserData_
validate *validator.Validate
}
// UpdateBalance implements users.UserService_
func (userService *userService) UpdateBalance(userID uint, input users.UserEntity) error <span class="cov8" title="1">{
if input.Balance < 1 </span><span class="cov8" title="1">{
return errors.New(consts.USER_InvalidInput)
}</span>
// get user balance
<span class="cov8" title="1">userEntity, errSelect := userService.userData.SelectData(userID)
if errSelect != nil </span><span class="cov8" title="1">{
return errSelect
}</span>
<span class="cov8" title="1">input.Balance += userEntity.Balance
errUpdate := userService.userData.UpdateData(userID, input)
if errUpdate != nil </span><span class="cov8" title="1">{
return errUpdate
}</span>
<span class="cov8" title="1">return nil</span>
}
// Create implements users.UserServiceInterface_
func (userService *userService) Create(input users.UserEntity) error <span class="cov8" title="1">{
errValidate := userService.validate.Struct(input)
if errValidate != nil </span><span class="cov8" title="1">{
return errValidate
}</span>
<span class="cov8" title="1">hashedPassword, errHash := helpers.HashPassword(input.Password)
if errHash != nil </span><span class="cov0" title="0">{
return errHash
}</span>
<span class="cov8" title="1">input.Password = hashedPassword
input.Balance = 0
errInsert := userService.userData.Insert(input)
if errInsert != nil </span><span class="cov8" title="1">{
return errInsert
}</span>
<span class="cov8" title="1">return nil</span>
}
// GetData implements users.UserServiceInterface_
func (userService *userService) GetData(userID uint) (users.UserEntity, error) <span class="cov8" title="1">{
userEntity, errSelect := userService.userData.SelectData(userID)
if errSelect != nil </span><span class="cov8" title="1">{
return users.UserEntity{}, errSelect
}</span>
<span class="cov8" title="1">return userEntity, nil</span>
}
// Login implements users.UserServiceInterface_
func (userService *userService) Login(email string, password string) (users.UserEntity, string, error) <span class="cov8" title="1">{
if email == "" || password == "" </span><span class="cov8" title="1">{
return users.UserEntity{}, "", errors.New(consts.USER_EmptyCredentialError)
}</span>
<span class="cov8" title="1">userEntity, errLogin := userService.userData.Login(email)
if errLogin != nil </span><span class="cov8" title="1">{
return users.UserEntity{}, "", errLogin
}</span>
<span class="cov8" title="1">if !helpers.CompareHashPassword(password, userEntity.Password) </span><span class="cov8" title="1">{
return users.UserEntity{}, "", errors.New(consts.USER_WrongPassword)
}</span>
<span class="cov8" title="1">token, errToken := middlewares.CreateToken(userEntity.ID)
if errToken != nil </span><span class="cov0" title="0">{
return users.UserEntity{}, "", errToken
}</span>
<span class="cov8" title="1">return userEntity, token, nil</span>
}
// ModifyData implements users.UserServiceInterface_
func (userService *userService) ModifyData(userID uint, input users.UserEntity) error <span class="cov8" title="1">{
errValidate := userService.validate.StructExcept(input, "Password")
if errValidate != nil </span><span class="cov8" title="1">{
return errValidate
}</span>
<span class="cov8" title="1">errUpdate := userService.userData.UpdateData(userID, input)
if errUpdate != nil </span><span class="cov8" title="1">{
return errUpdate
}</span>
<span class="cov8" title="1">return nil</span>
}
// ModifyPassword implements users.UserServiceInterface_
func (userService *userService) ModifyPassword(userID uint, input users.UserEntity) error <span class="cov8" title="1">{
if input.Password == "" || input.NewPassword == "" </span><span class="cov8" title="1">{
return errors.New(consts.USER_EmptyUpdatePasswordError)
}</span>
<span class="cov8" title="1">userEntity, errSelect := userService.userData.SelectData(userID)
if errSelect != nil </span><span class="cov8" title="1">{
return errSelect
}</span>
<span class="cov8" title="1">if !helpers.CompareHashPassword(input.Password, userEntity.Password) </span><span class="cov8" title="1">{
return errors.New(consts.USER_WrongPassword)
}</span>
<span class="cov8" title="1">hashedPassword, errHash := helpers.HashPassword(input.NewPassword)
if errHash != nil </span><span class="cov0" title="0">{
return errHash
}</span>
<span class="cov8" title="1">input.Password = hashedPassword
errUpdatePassword := userService.userData.UpdateData(userID, input)
if errUpdatePassword != nil </span><span class="cov8" title="1">{
return errUpdatePassword
}</span>
<span class="cov8" title="1">return nil</span>
}
// Remove implements users.UserServiceInterface_
func (userService *userService) Remove(userID uint) error <span class="cov8" title="1">{
errDelete := userService.userData.Delete(userID)
if errDelete != nil </span><span class="cov8" title="1">{
return errDelete
}</span>
<span class="cov8" title="1">return nil</span>
}
func New(userData users.UserData_) users.UserService_ <span class="cov8" title="1">{
return &userService{
userData: userData,
validate: validator.New(),
}
}</span>
</pre>
</div>
</body>
<script>
(function() {
var files = document.getElementById('files');
var visible;
files.addEventListener('change', onChange, false);
function select(part) {
if (visible)
visible.style.display = 'none';
visible = document.getElementById(part);
if (!visible)
return;
files.value = part;
visible.style.display = 'block';
location.hash = part;
}
function onChange() {
select(files.value);
window.scrollTo(0, 0);
}
if (location.hash != "") {
select(location.hash.substr(1));
}
if (!visible) {
select("file0");
}
})();
</script>
</html>