Skip to content

Commit

Permalink
Implemented the GetAllByCategory functionality for the property model
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan923 committed Feb 13, 2024
1 parent e45a90a commit d59d9b7
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 22 deletions.
7 changes: 7 additions & 0 deletions backend/src/api/dto/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dto

type LocationDto struct {
Country string `json:"country"`
State string `json:"state"`
City string `json:"city"`
}
27 changes: 27 additions & 0 deletions backend/src/api/dto/post.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dto

type PostDto struct {
Title string `json:"title"`
Description string `json:"description"`
PropertyId uint `json:"propertyId"`
}

type PostCreationDto struct {
Title string `json:"title"`
Description string `json:"description"`
}

type PostCommentDto struct {
Id uint `json:"id"`
Description string `json:"description"`
}

type PostCommentCreationDto struct {
Description string `json:"description"`
PostId uint `json:"postId"`
}

type AnnounceCreationDto struct {
Property PropertyCreationDto `json:"property"`
Post PostCommentCreationDto `json:"post"`
}
14 changes: 0 additions & 14 deletions backend/src/api/dto/property.dto.go

This file was deleted.

23 changes: 23 additions & 0 deletions backend/src/api/dto/property.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dto

type PropertyDto struct {
Owner UserDetail `json:"owner"`
CurrentCurrency string `json:"currentCurrency"`
CurrentPrice float32 `json:"currentPrice"`
PropertyDetail PropertyDetailDto `json:"propertyDetail"`
Location LocationDto `json:"location"`
Announcement PostDto `json:"announcement"`
}

type PropertyCreationDto struct {
OwnerId uint `json:"ownerId"`
CurrentCurrency string `json:"currentCurrency"`
CurrentPrice float32 `json:"currentPrice"`
}

type PropertyDetailDto struct {
NumberOfRooms int `json:"numberOfRooms"`
NumberOfBathrooms int `json:"numberOfBathrooms"`
NumberOfKitchens int `json:"numberOfKitchens"`
NumberOfParkingSpaces int `json:"numberOfParkingSpaces"`
}
4 changes: 4 additions & 0 deletions backend/src/api/handler/property_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ func (handler *PropertyHandler) GetAllByCategory(context *gin.Context, category

context.JSON(http.StatusOK, response2.GenerateResponse(authDetail, true))
}

func (handler *PropertyHandler) CreateProperty(context *gin.Context) {

}
2 changes: 1 addition & 1 deletion backend/src/data/database/migration/1_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func createAllTables(database *gorm.DB) {

addTable(database, &tables, model2.PropertyCategory{})
addTable(database, &tables, model2.Property{})
addTable(database, &tables, model2.PropertyDetails{})
addTable(database, &tables, model2.PropertyDetail{})
addTable(database, &tables, model2.PropertyPrice{})

addTable(database, &tables, model2.Post{})
Expand Down
2 changes: 2 additions & 0 deletions backend/src/data/model/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type Post struct {
type PostComment struct {
BaseModel
Description string `gorm:"type:string; size:512; not null;"`
UserId uint
User User `gorm:"foreignKey:UserId; constraint:OnUpdate:NO ACTION; OnDelete:NO ACTION;"`
PostId uint
Post Post `gorm:"foreignKey:PostId; constraint:OnUpdate:NO ACTION; OnDelete:NO ACTION;"`
}
4 changes: 2 additions & 2 deletions backend/src/data/model/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Property struct {
OwnerId uint
Owner User `gorm:"foreignKey:OwnerId; constraint:OnUpdate:NO ACTION; OnDelete:NO ACTION;"`
CityId uint
City User `gorm:"foreignKey:CityId; constraint:OnUpdate:NO ACTION; OnDelete:NO ACTION;"`
City City `gorm:"foreignKey:CityId; constraint:OnUpdate:NO ACTION; OnDelete:NO ACTION;"`
CurrentCurrency string `gorm:"size:4; type:string; not null;"`
CurrentPrice float32 `gorm:"type:decimal(10,2); not null;"`
Prices *[]PropertyPrice
Expand All @@ -25,7 +25,7 @@ type PropertyCategory struct {
Properties *[]Property
}

type PropertyDetails struct {
type PropertyDetail struct {
BaseModel
NumberOfRooms int `gorm:"type:int; not null;"`
NumberOfBathrooms int `gorm:"type:int; not null;"`
Expand Down
37 changes: 37 additions & 0 deletions backend/src/data/repository/property_detail_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package repository

import (
"github.com/Stefan923/go-estate-market/data/database"
"github.com/Stefan923/go-estate-market/data/model"
"github.com/Stefan923/go-estate-market/metrics"
"reflect"
)

type PropertyDetailRepository struct {
BaseRepository[model.PropertyDetail]
}

func NewPropertyDetailRepository() *PropertyDetailRepository {
return &PropertyDetailRepository{
BaseRepository: BaseRepository[model.PropertyDetail]{
Database: database.GetDatabase(),
Preloads: []PreloadSetting{},
},
}
}

func (repository *PropertyDetailRepository) FindByPropertyId(propertyId uint) (*model.PropertyDetail, error) {
var propertyDetail *model.PropertyDetail

err := repository.Database.
Where("property_id = ? and deleted_at is null", propertyId).
First(propertyDetail).
Error
if err != nil {
metrics.DatabaseCallCounter.WithLabelValues(reflect.TypeOf(*propertyDetail).String(), "FindByPropertyId", "Failed").Inc()
return nil, err
}

metrics.DatabaseCallCounter.WithLabelValues(reflect.TypeOf(*propertyDetail).String(), "FindByPropertyId", "Success").Inc()
return propertyDetail, nil
}
8 changes: 7 additions & 1 deletion backend/src/data/repository/property_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ func NewPropertyRepository() *PropertyRepository {
BaseRepository: BaseRepository[model.Property]{
Database: database.GetDatabase(),
Preloads: []PreloadSetting{
{EntityName: "Category"},
{EntityName: "PropertyCategory"},
{EntityName: "UserAccount"},
{EntityName: "User"},
{EntityName: "Country"},
{EntityName: "State"},
{EntityName: "City"},
{EntityName: "Post"},
},
},
}
Expand Down
55 changes: 51 additions & 4 deletions backend/src/service/property_service.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package service

import (
"github.com/Stefan923/go-estate-market/api/dto"
"github.com/Stefan923/go-estate-market/data/model"
"github.com/Stefan923/go-estate-market/data/pagination"
"github.com/Stefan923/go-estate-market/data/repository"
error3 "github.com/Stefan923/go-estate-market/error"
)

type PropertyService struct {
BaseService[model.Property, model.Property, model.Property, model.Property]
propertyRepository *repository.PropertyRepository
propertyRepository *repository.PropertyRepository
propertyDetailRepository *repository.PropertyDetailRepository
}

func NewPropertyService() *PropertyService {
Expand All @@ -17,10 +20,54 @@ func NewPropertyService() *PropertyService {
BaseService: BaseService[model.Property, model.Property, model.Property, model.Property]{
Repository: &propertyRepository.BaseRepository,
},
propertyRepository: propertyRepository,
propertyRepository: propertyRepository,
propertyDetailRepository: repository.NewPropertyDetailRepository(),
}
}

func (service *PropertyService) GetAllByCategory(category string, pageInfo *pagination.PageInfo) (*pagination.Page[model.Property], error) {
return service.propertyRepository.FindAllByCategory(category, pageInfo)
func (service *PropertyService) GetAllByCategory(category string, pageInfo *pagination.PageInfo) (*pagination.Page[dto.PropertyDto], error) {
properties, err := service.propertyRepository.FindAllByCategory(category, pageInfo)
if err != nil {
return nil, &error3.InternalError{EndUserMessage: error3.RecordNotFound}
}

var mappedProperties *pagination.Page[dto.PropertyDto]
for _, property := range *properties.Elements {
propertyDetail, err := service.propertyDetailRepository.FindByPropertyId(property.Id)
if err != nil {
return nil, &error3.InternalError{EndUserMessage: error3.RecordNotFound}
}

mappedProperty := dto.PropertyDto{
Owner: dto.UserDetail{
Email: property.Owner.UserAccount.Email,
FirstName: property.Owner.FirstName,
LastName: property.Owner.LastName,
},
CurrentCurrency: property.CurrentCurrency,
CurrentPrice: property.CurrentPrice,
PropertyDetail: dto.PropertyDetailDto{
NumberOfRooms: propertyDetail.NumberOfRooms,
NumberOfBathrooms: propertyDetail.NumberOfBathrooms,
NumberOfKitchens: propertyDetail.NumberOfKitchens,
NumberOfParkingSpaces: propertyDetail.NumberOfParkingSpaces,
},
Location: dto.LocationDto{
Country: property.City.State.Country.Name,
State: property.City.State.Name,
City: property.City.Name,
},
Announcement: dto.PostDto{
Title: property.Announcement.Title,
Description: property.Announcement.Description,
PropertyId: property.Id,
},
}
*mappedProperties.Elements = append(*mappedProperties.Elements, mappedProperty)
}

mappedProperties.PageNumber = properties.PageNumber
mappedProperties.PageSize = properties.PageSize

return mappedProperties, nil
}

0 comments on commit d59d9b7

Please sign in to comment.