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
15 changes: 15 additions & 0 deletions internal/app/model/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ 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 +47,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
}
42 changes: 41 additions & 1 deletion 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 @@ -34,5 +36,43 @@ func (s *server) createParcel(w http.ResponseWriter, r *http.Request) {
return
}

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

func (s *server) addCarrierRequest(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
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 request is on process")
}

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}/request").HandlerFunc(s.addCarrierRequest)

return r
}

Expand Down