diff --git a/backend/src/api/api.go b/backend/src/api/api.go index 42e9ed4..fb7be49 100644 --- a/backend/src/api/api.go +++ b/backend/src/api/api.go @@ -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())) diff --git a/backend/src/api/handler/property_handler.go b/backend/src/api/handler/property_handler.go index 01982b6..ff1a079 100644 --- a/backend/src/api/handler/property_handler.go +++ b/backend/src/api/handler/property_handler.go @@ -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" @@ -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)) } diff --git a/backend/src/api/router/base_router.go b/backend/src/api/router/base_router.go index 455b98a..49d43c7 100644 --- a/backend/src/api/router/base_router.go +++ b/backend/src/api/router/base_router.go @@ -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) +} diff --git a/backend/src/service/property_service.go b/backend/src/service/property_service.go index f06b8ca..42b55bd 100644 --- a/backend/src/service/property_service.go +++ b/backend/src/service/property_service.go @@ -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,