Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add delete shipper endpoint #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func main() {
router.HandleFunc("/shipper/{id}", sh.GetShipperHandler).Methods(http.MethodGet)
router.HandleFunc("/shippers", sh.GetShipperAllHandler).Methods(http.MethodGet)
router.HandleFunc("/", sh.RootHandler).Methods(http.MethodGet)
router.HandleFunc("/shipper/{id}", sh.DeleteShipperHandler).Methods(http.MethodDelete)

serverConfig := server.Config{
WriteTimeout: 5 * time.Second,
Expand Down
21 changes: 21 additions & 0 deletions service/server/handlers/shipper/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,24 @@ func (p *Handler) UpdateShipperHandler(w http.ResponseWriter, r *http.Request) {
server.RenderResponse(w, http.StatusCreated, resp, timeStart)
return
}

func (p *Handler) DeleteShipperHandler(w http.ResponseWriter, r *http.Request) {
log.Println("Entering DeleteShipper Handler")
timeStart := time.Now()
vars := mux.Vars(r)
queryID, err := strconv.ParseInt(vars["id"], 10, 64)
if err != nil {
log.Println("[ShipperHandler][DeleteShipper] bad request, err: ", err.Error())
server.RenderError(w, http.StatusBadRequest, err, timeStart)
return
}

resp, err := p.shipper.DeleteShipper(context.Background(), queryID)
if err != nil {
server.RenderError(w, http.StatusBadRequest, err, timeStart)
return
}

server.RenderResponse(w, http.StatusOK, resp, timeStart)
return
}
9 changes: 9 additions & 0 deletions service/shippermodule/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,13 @@ const (
id=$9
returning id
`

deleteShipperQuery = `
DELETE FROM
shipper
WHERE
id=$1
RETURNING
id
`
)
21 changes: 21 additions & 0 deletions service/shippermodule/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,24 @@ func (s *storage) UpdateShipper(ctx context.Context, id int64, param m.ShipperRe

return
}

func (s *storage) DeleteShipper(ctx context.Context, id int64) (result m.ShipperResponse, err error) {
res, err := s.ShipperDB.ExecContext(ctx, deleteShipperQuery, id)
if err != nil {
log.Println("[ShipperModule][DeleteShipper][Storage] problem querying to db, err: ", err.Error())
return
}

rowsAffected, err := res.RowsAffected()
if err != nil {
log.Println("[ShipperModule][DeleteShipper] problem querying to db, err: ", err.Error())
return
}
if rowsAffected == 0 {
log.Println("[ShipperModule][DeleteShipper] no rows affected in db")
return
}

result.ID = id
return
}
10 changes: 10 additions & 0 deletions service/shippermodule/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,13 @@ func (p *Module) UpdateShipper(ctx context.Context, id int64, data m.ShipperRequ

return
}

func (p *Module) DeleteShipper(ctx context.Context, id int64) (result m.ShipperResponse, err error) {
result, err = p.Storage.DeleteShipper(ctx, id)
if err != nil {
log.Println("[ShipperModule][DeleteShipper] problem getting storage data, err: ", err.Error())
return
}

return
}
2 changes: 1 addition & 1 deletion service/shippermodule/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func BuildQuery(id int64, param m.ShipperRequest) (finalQuery string, fieldValue
fieldValues = append(fieldValues, param.UpdatedAt)
i++

finalQuery = fmt.Sprintf(updateShipperQuery, fieldQuery[:len(fieldQuery)-1], id)
finalQuery = fmt.Sprint(updateShipperQuery, fieldQuery[:len(fieldQuery)-1], id)

return
}