Skip to content
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
16 changes: 16 additions & 0 deletions internal/app/model/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ type Parcel struct {
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}

type CarrierRequest struct {
ID int `json:"id"`
ParcelID int `json:"parcel_id" db:"parcel_id"`
CarrierID int `json:"carrier_id" db:"carrier_id"`
Status int `json:"status" db:"status"`
}


func (p *Parcel) ValidateParcelInput() error {
if p.SourceAddress == "" {
return fmt.Errorf("source Address is required :%w", ErrEmpty)
Expand All @@ -40,3 +48,11 @@ func (p *Parcel) ValidateParcelInput() error {

return nil
}

// Validates carrier request input credentials
func (cr *CarrierRequest) ValidateCarrierId() error {
if cr.CarrierID == 0 {
return fmt.Errorf("Carrier ID is required :%w", ErrEmpty)
}
return nil
}
40 changes: 40 additions & 0 deletions internal/app/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package server
import (
"CreateParcelApi/internal/app/model"
"encoding/json"
"github.com/gorilla/mux"
"github.com/rs/zerolog/log"
"net/http"
"strconv"
)

func (s *server) createParcel(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -36,3 +38,41 @@ func (s *server) createParcel(w http.ResponseWriter, r *http.Request) {

SuccessResponse(w, http.StatusOK, "your parcel is being created withing some moments")
}

func (s *server) parcelCarrierAccept(w http.ResponseWriter, r *http.Request) {
var data model.CarrierRequest
vars := mux.Vars(r)

if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
ErrUnprocessableEntityResponse(w, "Decode Error", err)
return
}
parcelID, err := strconv.Atoi(vars["id"])
if err != nil {
ErrInvalidEntityResponse(w, "Invalid Parcel ID", err)
return
}
data.ParcelID = parcelID

// validating input credentials for parcel request
if err := data.ValidateCarrierId(); err != nil {
ErrInvalidEntityResponse(w, "Invalid Input", err)
return
}

message, err := json.Marshal(data)
if err != nil {
log.Error().Err(err).Msg("json marshal failed")
ErrUnprocessableEntityResponse(w, "bad request", err)
return
}

err = s.publisherService.Push(message)
if err != nil {
log.Error().Err(err).Msg("failed to publish message")
ErrInternalServerResponse(w, "Failed to publish message", err)
return
}

SuccessResponse(w, http.StatusOK, "your parcel accept request is being created withing some moments")
}
2 changes: 2 additions & 0 deletions internal/app/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func (s *server) route() *mux.Router {
//apiRoute := r.PathPrefix("/api/v1").Subrouter()
r.Methods(http.MethodGet).Path("/ping").HandlerFunc(s.pingHandler)
r.Methods(http.MethodPost).Path("/api/v1/parcel").HandlerFunc(s.createParcel)
r.Methods(http.MethodPost).Path("/api/v1/parcel/{id}/accept").HandlerFunc(s.parcelCarrierAccept)

return r
}

Expand Down