Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

feat: Add extra components features #83

Merged
merged 4 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/model/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type Component struct {

Content any `gorm:"type:jsonb;not null"`

Price int `gorm:"default:0"`
Price int `gorm:"default:0"`
Budget int `gorm:"default:0"`
}

type ComponentOwner struct {
Expand Down
18 changes: 14 additions & 4 deletions internal/model/dto/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ type ComponentCreation struct {

Content any `validate:"required" json:"content"`

Price int `validate:"omitempty" json:"price"`
Price int `validate:"omitempty" json:"price"`
Budget int `validate:"omitempty" json:"budget"`

OwnerID uint `json:"-"` // Set using JWT

Expand All @@ -26,7 +27,8 @@ type ComponentUpdate struct {

Content *any `validate:"omitempty" json:"content"`

Price *int `validate:"omitempty,min=0,max=1000000" json:"price"`
Price *int `validate:"omitempty,min=0,max=1000000" json:"price"`
Budget *int `validate:"omitempty,min=0" json:"budget"`

Public *bool `validate:"omitempty" json:"public"`
}
Expand All @@ -43,9 +45,13 @@ type ComponentInfoJSON struct {
Content utils.JSON `json:"content"`

OwnerID uint `json:"owner_id"`
OwnerFirstName string `json:"owner_firstname"`
OwnerLastName string `json:"owner_lastname"`
OwnerUsername string `json:"owner_username"`
OwnerProfilePicture string `json:"owner_profile_picture"`
OwnerProfilePicture string `json:"owner_pfp"`
OwnerVerified bool `json:"owner_verified"`

Budget int `json:"budget"`
Price int `json:"price"`
PaidPrice *int `json:"paid_price"`
SellPrice *int `json:"sell_price"`
Expand All @@ -69,9 +75,13 @@ type ComponentInfo struct {
Content any `json:"content"`

OwnerID uint `json:"owner_id"`
OwnerFirstName string `json:"owner_firstname"`
OwnerLastName string `json:"owner_lastname"`
OwnerUsername string `json:"owner_username"`
OwnerProfilePicture string `json:"owner_profile_picture"`
OwnerProfilePicture string `json:"owner_pfp"`
OwnerVerified bool `json:"owner_verified"`

Budget int `json:"budget"`
Price int `json:"price"`
PaidPrice *int `json:"paid_price"`
SellPrice *int `json:"sell_price"`
Expand Down
26 changes: 25 additions & 1 deletion internal/service/repository/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,14 @@ func (cr *componentRepository) baseComponentQuery(issuerID uint) *gorm.DB {
c.description as description,
c.content as content,
c.price as price,
c.budget as budget,
co.id AS owner_id,
u.id AS owner_id,
u.first_name AS owner_first_name,
u.last_name AS owner_last_name,
u.username AS owner_username,
u.profile_picture AS owner_profile_picture,
u.profile_picture AS owner_profile_picture,
u.verified AS owner_verified,
COALESCE((
SELECT COUNT(*)
FROM component_holders ch
Expand Down Expand Up @@ -138,12 +143,16 @@ func convertToComponentInfo(jsonInfo *dto.ComponentInfoJSON) (dto.ComponentInfo,
Name: jsonInfo.Name,
Description: jsonInfo.Description,
Content: content,
Budget: jsonInfo.Budget,
Price: jsonInfo.Price,
PaidPrice: jsonInfo.PaidPrice,
SellPrice: jsonInfo.SellPrice,
OwnerID: jsonInfo.OwnerID,
OwnerFirstName: jsonInfo.OwnerFirstName,
OwnerLastName: jsonInfo.OwnerLastName,
OwnerUsername: jsonInfo.OwnerUsername,
OwnerProfilePicture: jsonInfo.OwnerProfilePicture,
OwnerVerified: jsonInfo.OwnerVerified,
IsPublic: jsonInfo.IsPublic,
Holders: jsonInfo.Holders,
Bought: jsonInfo.Bought,
Expand Down Expand Up @@ -211,6 +220,7 @@ func (cr *componentRepository) Create(createModel *dto.ComponentCreation) error
Name: createModel.Name,
Description: createModel.Description,
Content: string(contentJSON),
Budget: createModel.Budget,
Price: createModel.Price,
}

Expand Down Expand Up @@ -284,6 +294,10 @@ func (cr *componentRepository) Update(componentID uint, updateModel *dto.Compone
updates["content"] = string(contentJSON)
}

if updateModel.Budget != nil {
updates["budget"] = *updateModel.Budget
}

if updateModel.Price != nil {
updates["price"] = *updateModel.Price
}
Expand Down Expand Up @@ -328,14 +342,20 @@ func (cr *componentRepository) Get(issuerID uint, componentModel *model.Componen

owner = dto.UserInfoLite{
ID: ownerProfile.ID,
FirstName: ownerProfile.FirstName,
LastName: ownerProfile.LastName,
Username: ownerProfile.Username,
ProfilePicture: ownerProfile.ProfilePicture,
Verified: ownerProfile.Verified,
}
} else {
owner = dto.UserInfoLite{
ID: 0,
FirstName: "",
LastName: "",
Username: "",
ProfilePicture: "",
Verified: false,
}
}

Expand Down Expand Up @@ -378,8 +398,12 @@ func (cr *componentRepository) Get(issuerID uint, componentModel *model.Componen
Description: component.Description,
Content: contentData,
OwnerID: owner.ID,
OwnerFirstName: owner.FirstName,
OwnerLastName: owner.LastName,
OwnerUsername: owner.Username,
OwnerProfilePicture: owner.ProfilePicture,
OwnerVerified: owner.Verified,
Budget: component.Budget,
Price: component.Price,
PaidPrice: paidPrice,
SellPrice: sellPrice,
Expand Down
Loading