diff --git a/service/server/handlers/shipper/handler.go b/service/server/handlers/shipper/handler.go index 95a44e4..dd093c1 100644 --- a/service/server/handlers/shipper/handler.go +++ b/service/server/handlers/shipper/handler.go @@ -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.StatusCreated, resp, timeStart) + return +} diff --git a/service/shippermodule/query.go b/service/shippermodule/query.go index 53c2306..ef6c9e2 100644 --- a/service/shippermodule/query.go +++ b/service/shippermodule/query.go @@ -61,4 +61,9 @@ const ( id=$9 returning id ` + + deleteShipperQuery = ` + DELETE shipper WHERE id=$1 + returning id + ` ) diff --git a/service/shippermodule/storage.go b/service/shippermodule/storage.go index 8c48b48..949fe7c 100644 --- a/service/shippermodule/storage.go +++ b/service/shippermodule/storage.go @@ -121,3 +121,27 @@ 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][UpdateShipper][Storage] problem querying to db, err: ", err.Error()) + return + } + + rowsAffected, err := res.RowsAffected() + if err != nil { + log.Println("[ShipperModule][UpdateShipper] problem querying to db, err: ", err.Error()) + return + } + if rowsAffected == 0 { + log.Println("[ShipperModule][UpdateShipper] no rows affected in db") + return + } + + result.ID = id + + return +} diff --git a/service/shippermodule/usecase.go b/service/shippermodule/usecase.go index 634ef96..24e0d44 100644 --- a/service/shippermodule/usecase.go +++ b/service/shippermodule/usecase.go @@ -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 +}