Skip to content

Commit

Permalink
Merge pull request #24 from gbrayhan/add-all-medicines-endpoint
Browse files Browse the repository at this point in the history
add All medicines endpoint full process
gbrayhan authored Jan 27, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 35d7a50 + 0db5420 commit d8746bf
Showing 8 changed files with 155 additions and 42 deletions.
57 changes: 42 additions & 15 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -467,32 +467,26 @@ const docTemplate = `{
"github_com_gbrayhan_microservices-go_src_domain_medicine.Medicine": {
"type": "object",
"properties": {
"created_at": {
"createdAt": {
"type": "string"
},
"description": {
"type": "string",
"example": "Some Description"
"type": "string"
},
"ean_code": {
"type": "string",
"example": "9900000124"
"eanCode": {
"type": "string"
},
"id": {
"type": "integer",
"example": 123
"type": "integer"
},
"laboratory": {
"type": "string",
"example": "Roche"
"type": "string"
},
"name": {
"type": "string",
"example": "Paracetamol"
"type": "string"
},
"updated_at": {
"type": "string",
"example": "2021-02-24 20:19:39"
"updatedAt": {
"type": "string"
}
}
},
@@ -531,6 +525,39 @@ const docTemplate = `{
}
}
},
"medicine.ResponseMedicine": {
"type": "object",
"properties": {
"createdAt": {
"type": "string",
"example": "2021-02-24 20:19:39"
},
"description": {
"type": "string",
"example": "Some Description"
},
"eanCode": {
"type": "string",
"example": "Some EanCode"
},
"id": {
"type": "integer",
"example": 1099
},
"laboratory": {
"type": "string",
"example": "Some Laboratory"
},
"name": {
"type": "string",
"example": "Aspirina"
},
"updatedAt": {
"type": "string",
"example": "2021-02-24 20:19:39"
}
}
},
"user.MessageResponse": {
"type": "object",
"properties": {
57 changes: 42 additions & 15 deletions docs/swagger.json
Original file line number Diff line number Diff line change
@@ -461,32 +461,26 @@
"github_com_gbrayhan_microservices-go_src_domain_medicine.Medicine": {
"type": "object",
"properties": {
"created_at": {
"createdAt": {
"type": "string"
},
"description": {
"type": "string",
"example": "Some Description"
"type": "string"
},
"ean_code": {
"type": "string",
"example": "9900000124"
"eanCode": {
"type": "string"
},
"id": {
"type": "integer",
"example": 123
"type": "integer"
},
"laboratory": {
"type": "string",
"example": "Roche"
"type": "string"
},
"name": {
"type": "string",
"example": "Paracetamol"
"type": "string"
},
"updated_at": {
"type": "string",
"example": "2021-02-24 20:19:39"
"updatedAt": {
"type": "string"
}
}
},
@@ -525,6 +519,39 @@
}
}
},
"medicine.ResponseMedicine": {
"type": "object",
"properties": {
"createdAt": {
"type": "string",
"example": "2021-02-24 20:19:39"
},
"description": {
"type": "string",
"example": "Some Description"
},
"eanCode": {
"type": "string",
"example": "Some EanCode"
},
"id": {
"type": "integer",
"example": 1099
},
"laboratory": {
"type": "string",
"example": "Some Laboratory"
},
"name": {
"type": "string",
"example": "Aspirina"
},
"updatedAt": {
"type": "string",
"example": "2021-02-24 20:19:39"
}
}
},
"user.MessageResponse": {
"type": "object",
"properties": {
36 changes: 27 additions & 9 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -70,25 +70,19 @@ definitions:
type: object
github_com_gbrayhan_microservices-go_src_domain_medicine.Medicine:
properties:
created_at:
createdAt:
type: string
description:
example: Some Description
type: string
ean_code:
example: "9900000124"
eanCode:
type: string
id:
example: 123
type: integer
laboratory:
example: Roche
type: string
name:
example: Paracetamol
type: string
updated_at:
example: "2021-02-24 20:19:39"
updatedAt:
type: string
type: object
medicine.MessageResponse:
@@ -116,6 +110,30 @@ definitions:
- laboratory
- name
type: object
medicine.ResponseMedicine:
properties:
createdAt:
example: "2021-02-24 20:19:39"
type: string
description:
example: Some Description
type: string
eanCode:
example: Some EanCode
type: string
id:
example: 1099
type: integer
laboratory:
example: Some Laboratory
type: string
name:
example: Aspirina
type: string
updatedAt:
example: "2021-02-24 20:19:39"
type: string
type: object
user.MessageResponse:
properties:
message:
7 changes: 6 additions & 1 deletion src/application/usecases/medicine/medicine.go
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ var _ medicineDomain.Service = &Service{}

// GetData is a function that returns all medicines
func (s *Service) GetData(page int64, limit int64, sortBy string, sortDirection string, filters map[string][]string, searchText string, dateRangeFilters []domain.DateRangeFilter) (*medicineDomain.DataMedicine, error) {
return s.MedicineRepository.GetAll(page, limit, sortBy, sortDirection, filters, searchText, dateRangeFilters)
return s.MedicineRepository.GetData(page, limit, sortBy, sortDirection, filters, searchText, dateRangeFilters)
}

// GetByID is a function that returns a medicine by id
@@ -44,3 +44,8 @@ func (s *Service) Delete(id int) error {
func (s *Service) Update(id int, medicineMap map[string]any) (*medicineDomain.Medicine, error) {
return s.MedicineRepository.Update(id, medicineMap)
}

// GetAll is a function that returns all medicines
func (s *Service) GetAll() (*[]medicineDomain.Medicine, error) {
return s.MedicineRepository.GetAll()
}
1 change: 1 addition & 0 deletions src/domain/medicine/Medicine.go
Original file line number Diff line number Diff line change
@@ -42,6 +42,7 @@ type DataMedicine struct {

// Service is a interface that contains the methods for the medicine service
type Service interface {
GetAll() (*[]Medicine, error)
GetData(page int64, limit int64, sortBy string, sortDirection string, filters map[string][]string, searchText string, dateRangeFilters []domain.DateRangeFilter) (*DataMedicine, error)
GetByID(id int) (*Medicine, error)
Create(medicine *NewMedicine) (*Medicine, error)
16 changes: 14 additions & 2 deletions src/infrastructure/repository/medicine/medicine.go
Original file line number Diff line number Diff line change
@@ -37,8 +37,8 @@ var ColumnsMedicineStructure = map[string]string{
"updatedAt": "UpdatedAt",
}

// GetAll Fetch all medicine data
func (r *Repository) GetAll(page int64, limit int64, sortBy string, sortDirection string, filters map[string][]string, searchText string, dateRangeFilters []domain.DateRangeFilter) (*domainMedicine.DataMedicine, error) {
// GetData Fetch all medicine data
func (r *Repository) GetData(page int64, limit int64, sortBy string, sortDirection string, filters map[string][]string, searchText string, dateRangeFilters []domain.DateRangeFilter) (*domainMedicine.DataMedicine, error) {
var users []Medicine
var total int64
offset := (page - 1) * limit
@@ -195,3 +195,15 @@ func (r *Repository) Delete(id int) (err error) {

return
}

// GetAll Fetch all medicine data no params just all data
func (r *Repository) GetAll() (*[]domainMedicine.Medicine, error) {
var medicines []Medicine
err := r.DB.Find(&medicines).Error
if err != nil {
err = domainErrors.NewAppErrorWithType(domainErrors.UnknownError)
return nil, err
}

return arrayToDomainMapper(&medicines), nil
}
21 changes: 21 additions & 0 deletions src/infrastructure/rest/controllers/medicine/Medicines.go
Original file line number Diff line number Diff line change
@@ -57,6 +57,27 @@ func (c *Controller) NewMedicine(ctx *gin.Context) {
ctx.JSON(http.StatusOK, responseMedicine)
}

// GetAllMedicines godoc
// @Tags medicine
// @Summary Get all Medicines
// @Description Get all Medicines on the system
// @Success 200 {object} []ResponseMedicine
// @Failure 400 {object} MessageResponse
// @Failure 500 {object} MessageResponse
// @Router /medicine [get]
func (c *Controller) GetAllMedicines(ctx *gin.Context) {
medicines, err := c.MedicineService.GetAll()

if err != nil {
appError := domainError.NewAppErrorWithType(domainError.UnknownError)
_ = ctx.Error(appError)
return
}

ctx.JSON(http.StatusOK, arrayDomainToResponseMapper(medicines))

}

// GetDataMedicines godoc
// @Tags medicine
// @Summary Get all Medicines
2 changes: 2 additions & 0 deletions src/infrastructure/rest/routes/medicine.go
Original file line number Diff line number Diff line change
@@ -14,11 +14,13 @@ func MedicineRoutes(router *gin.RouterGroup, controller *medicineController.Cont
routerMedicine.Use(middlewares.AuthJWTMiddleware())

{
routerMedicine.GET("/", controller.GetAllMedicines)
routerMedicine.POST("/", controller.NewMedicine)
routerMedicine.GET("/:id", controller.GetMedicinesByID)
routerMedicine.POST("/data", controller.GetDataMedicines)
routerMedicine.PUT("/:id", controller.UpdateMedicine)
routerMedicine.DELETE("/:id", controller.DeleteMedicine)

}

}

0 comments on commit d8746bf

Please sign in to comment.