Skip to content

Commit

Permalink
Implemented a handler for creation of new properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan923 committed Feb 15, 2024
1 parent 6532c77 commit 00303ae
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
2 changes: 2 additions & 0 deletions backend/src/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ func registerRoutes(engine *gin.Engine, config *config.Config) {
v1Route := apiRoute.Group("/v1")
{
userAccountsRoute := v1Route.Group("/auth")
propertiesRoute := v1Route.Group("/properties")

router.StartAuthRouter(userAccountsRoute, config)
router.StartPropertiesRouter(propertiesRoute, config)
}

engine.GET("/metrics", gin.WrapH(promhttp.Handler()))
Expand Down
18 changes: 18 additions & 0 deletions backend/src/api/handler/property_handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handler

import (
"github.com/Stefan923/go-estate-market/api/dto"
response2 "github.com/Stefan923/go-estate-market/api/response"
"github.com/Stefan923/go-estate-market/data/pagination"
"github.com/Stefan923/go-estate-market/service"
Expand Down Expand Up @@ -30,5 +31,22 @@ func (handler *PropertyHandler) GetAllByCategory(context *gin.Context, category
}

func (handler *PropertyHandler) CreateProperty(context *gin.Context) {
request := new(dto.PropertyCreationDto)
err := context.ShouldBindJSON(request)
if err != nil {
context.AbortWithStatusJSON(
http.StatusBadRequest,
response2.GenerateResponseWithValidationError(nil, false, err))
return
}

createdProperty, err := handler.propertyService.Save(context, request)
if err != nil {
context.AbortWithStatusJSON(
response2.TranslateErrorToStatusCode(err),
response2.GenerateResponseWithError(nil, false, err))
return
}

context.JSON(http.StatusCreated, response2.GenerateResponse(createdProperty, false))
}
7 changes: 7 additions & 0 deletions backend/src/api/router/base_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ func StartAuthRouter(router *gin.RouterGroup, config *config.Config) {
router.POST("/login", userAccountHandler.Login)
router.POST("/register", userAccountHandler.Register)
}

func StartPropertiesRouter(router *gin.RouterGroup, config *config.Config) {
userAccountHandler := handler.NewUserAccountHandler(config)

router.GET("/", userAccountHandler.Login)
router.POST("/", userAccountHandler.Register)
}
2 changes: 1 addition & 1 deletion backend/src/service/property_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewPropertyService() *PropertyService {
}
}

func (service *PropertyService) Save(context context.Context, propertyDto dto.PropertyCreationDto) (*dto.PropertyDto, error) {
func (service *PropertyService) Save(context context.Context, propertyDto *dto.PropertyCreationDto) (*dto.PropertyDto, error) {
property := model.Property{
OwnerId: propertyDto.OwnerId,
CityId: propertyDto.CityId,
Expand Down

0 comments on commit 00303ae

Please sign in to comment.