diff --git a/server/api/tcp/handlers/restaurant_handler.go b/server/api/tcp/handlers/restaurant_handler.go index e1ad44b..f41f35f 100644 --- a/server/api/tcp/handlers/restaurant_handler.go +++ b/server/api/tcp/handlers/restaurant_handler.go @@ -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 { @@ -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 { @@ -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) -} \ No newline at end of file diff --git a/server/internal/protocol/tcp/message.go b/server/internal/protocol/tcp/message.go index cb3ed04..f9db70e 100644 --- a/server/internal/protocol/tcp/message.go +++ b/server/internal/protocol/tcp/message.go @@ -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"` -} \ No newline at end of file + 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"` +} diff --git a/server/internal/protocol/tcp/parser.go b/server/internal/protocol/tcp/parser.go index 09a893c..086f5d6 100644 --- a/server/internal/protocol/tcp/parser.go +++ b/server/internal/protocol/tcp/parser.go @@ -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) } \ No newline at end of file