6
6
"strconv"
7
7
8
8
"github.com/gin-gonic/gin"
9
+ "github.com/putukrisna6/golang-api/cache"
9
10
"github.com/putukrisna6/golang-api/dto"
10
11
"github.com/putukrisna6/golang-api/entity"
11
12
"github.com/putukrisna6/golang-api/helper"
@@ -22,11 +23,13 @@ type ReceiptController interface {
22
23
23
24
type receiptController struct {
24
25
receiptService service.ReceiptService
26
+ receiptCache cache.ReceiptCache
25
27
}
26
28
27
- func NewReceiptController (receiptService service.ReceiptService ) ReceiptController {
29
+ func NewReceiptController (receiptService service.ReceiptService , receiptCache cache. ReceiptCache ) ReceiptController {
28
30
return & receiptController {
29
31
receiptService : receiptService ,
32
+ receiptCache : receiptCache ,
30
33
}
31
34
}
32
35
@@ -44,11 +47,15 @@ func (c *receiptController) Show(context *gin.Context) {
44
47
return
45
48
}
46
49
47
- var receipt entity.Receipt = c .receiptService . Show ( id )
50
+ var receipt entity.Receipt = c .receiptCache . Get ( strconv . FormatUint ( id , 10 ) )
48
51
if (receipt == entity.Receipt {}) {
49
- res := helper .BuildErrorResponse ("failed to retrieve Receipt" , "no data with given receiptID" , helper.EmptyObj {})
50
- context .AbortWithStatusJSON (http .StatusNotFound , res )
51
- return
52
+ var receipt entity.Receipt = c .receiptService .Show (id )
53
+ if (receipt == entity.Receipt {}) {
54
+ res := helper .BuildErrorResponse ("failed to retrieve Receipt" , "no data with given receiptID" , helper.EmptyObj {})
55
+ context .AbortWithStatusJSON (http .StatusNotFound , res )
56
+ return
57
+ }
58
+ c .receiptCache .Set (strconv .FormatUint (id , 10 ), receipt )
52
59
}
53
60
54
61
res := helper .BuildValidResponse ("OK" , receipt )
0 commit comments