Skip to content

Commit

Permalink
Find airport by code without login
Browse files Browse the repository at this point in the history
  • Loading branch information
boozec committed Apr 30, 2024
1 parent 4c65b4b commit e097c28
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
10 changes: 5 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ func main() {
v1.POST("/login/", handlers.LoginHandler)
airports := v1.Group("/airports")
{
airports.Use(middleware.Auth())
airports.GET("/", handlers.AirportHandlerGet)
airports.POST("/", handlers.AirportHandlerPost)
airports.GET("/:id/", handlers.AirportHandlerGetId)
airports.PUT("/:id/", handlers.AirportHandlerPut)
airports.GET("/", middleware.Auth(), handlers.AirportHandlerGet)
airports.POST("/", middleware.Auth(), handlers.AirportHandlerPost)
airports.GET("/:id/", middleware.Auth(), handlers.AirportHandlerGetId)
airports.GET("/code/:code/", handlers.AirportHandlerGetCode)
airports.PUT("/:id/", middleware.Auth(), handlers.AirportHandlerPut)
}

flights := v1.Group("/flights")
Expand Down
29 changes: 28 additions & 1 deletion internal/handlers/airport.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
// Handle GET request for `Airport` model.
// It returns a list of airports.
// GetAirports godoc
//
// @Summary Get all airports
// @Schemes
// @Description Get all airports
Expand All @@ -35,6 +36,7 @@ func AirportHandlerGet(c *gin.Context) {
// Validate JSON input by the request and crate a new airport. Finally returns
// the new created data.
// PostAirports godoc
//
// @Summary Create a new airport
// @Schemes
// @Description Create a new airport
Expand All @@ -60,14 +62,15 @@ func AirportHandlerPost(c *gin.Context) {
// Handle GET request for a selected id.
// Returns the airport or a 404 status
// GetAirportById godoc
//
// @Summary Get an airport
// @Schemes
// @Description Get an airport
// @Tags Airports
// @Accept json
// @Produce json
// @Success 200
// @Router /v1/airports/{airportId}/ [post]
// @Router /v1/airports/{airportId}/ [get]
func AirportHandlerGetId(c *gin.Context) {
db, _ := db.GetDb()
var airport models.Airport
Expand All @@ -79,10 +82,34 @@ func AirportHandlerGetId(c *gin.Context) {
c.JSON(http.StatusOK, airport)
}

// Handle GET request for a selected id.
// Returns the airport by code or a 404 status
// GetAirportById godoc
//
// @Summary Get an airport
// @Schemes
// @Description Get an airport
// @Tags Airports
// @Accept json
// @Produce json
// @Success 200
// @Router /v1/airports/{airportCode}/ [get]
func AirportHandlerGetCode(c *gin.Context) {
db, _ := db.GetDb()
var airport models.Airport
if err := db.Where("lower(code) = lower(?)", c.Param("code")).First(&airport).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"message": err.Error()})
return
}

c.JSON(http.StatusOK, airport)
}

// Handle PUT request for `Airport` model.
// First checks if the selected airport exists or not. Then, validates JSON input by the
// request and edit a selected airport. Finally returns the new created data.
// EditAirportById godoc
//
// @Summary Edit an airport
// @Schemes
// @Description Edit an airport
Expand Down

0 comments on commit e097c28

Please sign in to comment.