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

Added Withdraw OwnerShip Of The Restaurant handler #45

Open
wants to merge 1 commit into
base: dev
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
90 changes: 59 additions & 31 deletions server/api/tcp/handlers/restaurant_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,60 @@ func (h *RestaurantHandler) HandleGetRestaurantsToAddMotorOperator(ctx context.C
tcp.SendResponse(conn, tcp.StatusOK, nil, resData)
}

func (h *RestaurantHandler)HandleAddMotorToRestaurant(ctx context.Context, conn net.Conn, req *tcp.Request){
reqData , err := tcp.DecodeAddMotorToRestaurantRequest(req.Data)
if err != nil {
//logger
fmt.Println("Error decoding add motors request:", err)
tcp.Error(conn, tcp.StatusBadRequest, nil, err.Error())
return
}
addedMotor , err := h.restaurantService.AddMotor(ctx,reqData.Motor,reqData.RestaurantID)
if err != nil {
tcp.Error(conn, tcp.StatusBadRequest, nil, err.Error())
return
}
response := tcp.AddMotorToRestaurantResponse{
Message: "Motor Susscefully Added.",
Motor: addedMotor,
}
resData,err := tcp.EncodeAddMotorToRestaurantResponse(response)
if err != nil {
//logger.Error("Error encoding register response:", err)
fmt.Println("Error encoding menu item response:", err)
tcp.Error(conn, tcp.StatusBadRequest, nil, err.Error())
return
}
tcp.SendResponse(conn, tcp.StatusCreated, nil, resData)
}

func (h *RestaurantHandler) HandleWithdrawOwnershipOfRestaurant(ctx context.Context, conn net.Conn, req *tcp.Request){
reqData , err := tcp.DecodeWithdrawOwnershipOfRestaurantRequest(req.Data)
if err != nil {
//logger
fmt.Println("Error decoding change of ownership request:", err)
tcp.Error(conn, tcp.StatusBadRequest, nil, err.Error())
return
}
err = h.restaurantService.WithdrawRestaurant(ctx,reqData.NewOwnerID,reqData.RestaurantID)
if err != nil {
tcp.Error(conn, tcp.StatusNotModified, nil, err.Error())
return
}
response := tcp.WithdrawOwnershipOfRestaurantResponse{
Message: "Changing Ownership Succsesfully Occured",
TakenRestaurant: restaurant.NewRestaurantByID(reqData.NewOwnerID),
}
resData,err := tcp.EncodeWithdrawOwnershipOfRestaurantResponse(response)
if err != nil {
//logger.Error("Error encoding register response:", err)
fmt.Println("Error encoding the OwnerShip Response:", err)
tcp.Error(conn, tcp.StatusBadRequest, nil, err.Error())
return
}
tcp.SendResponse(conn, tcp.StatusCreated, nil, resData)
}

func (h *RestaurantHandler) ServeTCP(ctx context.Context, conn net.Conn, TCPReq *tcp.Request) {
firstRoute, _ := utils.RouteSplitter(TCPReq.Location)
switch firstRoute {
Expand Down Expand Up @@ -528,9 +582,11 @@ func (h *RestaurantHandler) ServeTCP(ctx context.Context, conn net.Conn, TCPReq
getMenuItemsOfMenuHandler(ctx, conn, TCPReq)
return
}
case "withdraw":
//withdraw_ownership
fmt.Println("not implemented")
case "ownership":
if TCPReq.Header["method"] == tcp.MethodPost {
withdrawOwnershipOfRestaurantHandler := middleware.ApplyMiddlewares(h.HandleWithdrawOwnershipOfRestaurant , middleware.AuthMiddleware)
withdrawOwnershipOfRestaurantHandler(ctx,conn,TCPReq)
}
case "operators":
// (post, get, delete)
if TCPReq.Header["method"] == tcp.MethodPost {
Expand All @@ -548,31 +604,3 @@ func (h *RestaurantHandler) ServeTCP(ctx context.Context, conn net.Conn, TCPReq
}
tcp.Error(conn, tcp.StatusMethodNotAllowed, nil, "method not allowed.")
}


func (h *RestaurantHandler)HandleAddMotorToRestaurant(ctx context.Context, conn net.Conn, req *tcp.Request){
reqData , err := tcp.DecodeAddMotorToRestaurantRequest(req.Data)
if err != nil {
//logger
fmt.Println("Error decoding add categories request:", err)
tcp.Error(conn, tcp.StatusBadRequest, nil, err.Error())
return
}
addedMotor , err := h.restaurantService.AddMotor(ctx,reqData.Motor,reqData.RestaurantID)
if err != nil {
tcp.Error(conn, tcp.StatusBadRequest, nil, err.Error())
return
}
response := tcp.AddMotorToRestaurantResponse{
Message: "Motor Susscefully Added.",
Motor: addedMotor,
}
resData,err := tcp.EncodeAddMotorToRestaurantResponse(response)
if err != nil {
//logger.Error("Error encoding register response:", err)
fmt.Println("Error encoding menu item response:", err)
tcp.Error(conn, tcp.StatusBadRequest, nil, err.Error())
return
}
tcp.SendResponse(conn, tcp.StatusCreated, nil, resData)
}
15 changes: 11 additions & 4 deletions server/internal/protocol/tcp/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,18 @@ type RestaurantToAddCategoryMenuFoodResponse struct {
}

type AddMotorToRestaurantRequest struct {
Message string `json:"message"`
RestaurantID uint `json:"restaurant_id"`
Motor *motor.Motor `json:"motor"`
}
type AddMotorToRestaurantResponse struct {
Message string `json:"message"`
Motor *motor.Motor `json:"motor"`
}
Message string `json:"message"`
Motor *motor.Motor `json:"motor"`
}
type WithdrawOwnershipOfRestaurantRequest struct {
NewOwnerID uint `json:"new_owner_id"`
RestaurantID uint `json:"restaurant_id"`
}
type WithdrawOwnershipOfRestaurantResponse struct {
Message string `json:"message"`
TakenRestaurant *restaurant.Restaurant `json:"restaurant"`
}
8 changes: 8 additions & 0 deletions server/internal/protocol/tcp/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,12 @@ func DecodeAddMotorToRestaurantRequest(data []byte)(AddMotorToRestaurantRequest,
}
func EncodeAddMotorToRestaurantResponse(res AddMotorToRestaurantResponse) ([]byte, error) {
return json.Marshal(res)
}
func DecodeWithdrawOwnershipOfRestaurantRequest(data []byte)(WithdrawOwnershipOfRestaurantRequest,error){
var req WithdrawOwnershipOfRestaurantRequest
err := json.Unmarshal(data,&req)
return req,err
}
func EncodeWithdrawOwnershipOfRestaurantResponse(res WithdrawOwnershipOfRestaurantResponse) ([]byte, error) {
return json.Marshal(res)
}