Feat(booking): Added updateBookingStatus
All checks were successful
ktm-booking-bot/ktm-booking-bot/pipeline/head This commit looks good
All checks were successful
ktm-booking-bot/ktm-booking-bot/pipeline/head This commit looks good
This commit is contained in:
@@ -156,6 +156,51 @@ const docTemplate = `{
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/ktmtrainbot/booking/{bookingID}/{status}": {
|
||||
"put": {
|
||||
"description": "Description",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"ktmtrainbot Booking"
|
||||
],
|
||||
"summary": "Changing booking status",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "Booking ID",
|
||||
"name": "bookingID",
|
||||
"in": "path",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "Status",
|
||||
"name": "status",
|
||||
"in": "path",
|
||||
"required": true
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/ktmtrainbot.BookingResponse"
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad Request",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/common.ErrResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/ktmtrainbot/current-time": {
|
||||
"get": {
|
||||
"description": "Description",
|
||||
|
||||
@@ -148,6 +148,51 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/ktmtrainbot/booking/{bookingID}/{status}": {
|
||||
"put": {
|
||||
"description": "Description",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"ktmtrainbot Booking"
|
||||
],
|
||||
"summary": "Changing booking status",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "Booking ID",
|
||||
"name": "bookingID",
|
||||
"in": "path",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "Status",
|
||||
"name": "status",
|
||||
"in": "path",
|
||||
"required": true
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/ktmtrainbot.BookingResponse"
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad Request",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/common.ErrResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/ktmtrainbot/current-time": {
|
||||
"get": {
|
||||
"description": "Description",
|
||||
|
||||
@@ -267,6 +267,36 @@ paths:
|
||||
summary: Delete booking
|
||||
tags:
|
||||
- ktmtrainbot Booking
|
||||
/api/v1/ktmtrainbot/booking/{bookingID}/{status}:
|
||||
put:
|
||||
consumes:
|
||||
- application/json
|
||||
description: Description
|
||||
parameters:
|
||||
- description: Booking ID
|
||||
in: path
|
||||
name: bookingID
|
||||
required: true
|
||||
type: string
|
||||
- description: Status
|
||||
in: path
|
||||
name: status
|
||||
required: true
|
||||
type: string
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/ktmtrainbot.BookingResponse'
|
||||
"400":
|
||||
description: Bad Request
|
||||
schema:
|
||||
$ref: '#/definitions/common.ErrResponse'
|
||||
summary: Changing booking status
|
||||
tags:
|
||||
- ktmtrainbot Booking
|
||||
/api/v1/ktmtrainbot/current-time:
|
||||
get:
|
||||
description: Description
|
||||
|
||||
@@ -83,3 +83,34 @@ func (env *Env) deleteBooking(
|
||||
|
||||
return &newBooking, nil
|
||||
}
|
||||
|
||||
func (env *Env) updateBookingStatus(
|
||||
user *user.User,
|
||||
bookingIDStr string,
|
||||
status string,
|
||||
) (*Booking, error) {
|
||||
var newBooking Booking
|
||||
|
||||
bookingID, err := uuid.Parse(bookingIDStr)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil, errors.New("invalid uuid")
|
||||
}
|
||||
|
||||
err = env.DB.Where(&Booking{ID: bookingID}).Where("user_id = ?", user.ID).First(&newBooking).Error
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil, errors.New("failed retrieve booking")
|
||||
}
|
||||
|
||||
newBooking.Status = status
|
||||
|
||||
err = env.DB.Save(&newBooking).Error
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil, errors.New("failed to update booking status")
|
||||
}
|
||||
|
||||
return &newBooking, nil
|
||||
}
|
||||
|
||||
@@ -123,3 +123,37 @@ func (env *Env) deleteBookingRoute(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
render.Render(w, r, env.NewBookingResponse(booking))
|
||||
}
|
||||
|
||||
// Change booking status
|
||||
// @Summary Changing booking status
|
||||
// @Description Description
|
||||
// @Tags ktmtrainbot Booking
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param bookingID path string true "Booking ID"
|
||||
// @Param status path string true "Status"
|
||||
// @Success 200 {object} BookingResponse
|
||||
// @Failure 400 {object} common.ErrResponse
|
||||
// @Router /api/v1/ktmtrainbot/booking/{bookingID}/{status} [put]
|
||||
func (env *Env) updateBookingStatusRoute(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
currUser, ok := ctx.Value(user.UserContextKey).(*user.User)
|
||||
if !ok {
|
||||
err := errors.New("user not logged in")
|
||||
render.Render(w, r, common.ErrInternalError(err))
|
||||
return
|
||||
}
|
||||
_ = currUser
|
||||
|
||||
bookingID := chi.URLParam(r, "bookingID")
|
||||
status := chi.URLParam(r, "status")
|
||||
|
||||
booking, err := env.updateBookingStatus(currUser, bookingID, status)
|
||||
|
||||
if err != nil {
|
||||
render.Render(w, r, common.ErrInvalidRequest(err))
|
||||
return
|
||||
}
|
||||
|
||||
render.Render(w, r, env.NewBookingResponse(booking))
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ func KTMTrainBotRoutes(db *gorm.DB) chi.Router {
|
||||
checkLoggedInUserGroup.Get("/booking", env.getBookingRoute)
|
||||
checkLoggedInUserGroup.Post("/booking", env.createBookingRoute)
|
||||
checkLoggedInUserGroup.Delete("/booking/{bookingID}", env.deleteBookingRoute)
|
||||
checkLoggedInUserGroup.Put("/booking/{bookingID}/{status}", env.updateBookingStatusRoute)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user