Skip to content
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
1 change: 1 addition & 0 deletions cmd/seeder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func main() {
seeder.WithContext(ctx),
seeder.WithSeeder(seeder.NewRoleSeeder()),
seeder.WithSeeder(seeder.NewUserSeeder()),
seeder.WithSeeder(seeder.NewRaritySeeder()),
)

go func() {
Expand Down
93 changes: 93 additions & 0 deletions docs/swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,96 @@ const docTemplate = `{
}
}
},
"/profiles/{id}/pfp": {
"get": {
"description": "Returns an uploaded profile picture",
"consumes": [
"application/json"
],
"produces": [
"multipart/form-data"
],
"tags": [
"profiles"
],
"parameters": [
{
"type": "integer",
"description": "id of desired profile",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"404": {
"description": "record not found",
"schema": {
"$ref": "#/definitions/dtos.ErrResp"
}
},
"500": {
"description": "internal server error",
"schema": {
"$ref": "#/definitions/dtos.ErrResp"
}
}
}
},
"post": {
"description": "Uploads a profile picture",
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json"
],
"tags": [
"profiles"
],
"parameters": [
{
"type": "integer",
"description": "id of desired profile",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"201": {
"description": "returns newly created profile picture's URI",
"schema": {
"type": "string"
}
},
"400": {
"description": "no file sent",
"schema": {
"$ref": "#/definitions/dtos.ErrResp"
}
},
"401": {
"description": "unauthorized",
"schema": {
"$ref": "#/definitions/dtos.ErrResp"
}
},
"415": {
"description": "invalid media type",
"schema": {
"$ref": "#/definitions/dtos.ErrResp"
}
},
"500": {
"description": "internal server error",
"schema": {
"$ref": "#/definitions/dtos.ErrResp"
}
}
}
}
},
"/roles": {
"get": {
"description": "Reads all roles",
Expand Down Expand Up @@ -1884,6 +1974,9 @@ const docTemplate = `{
},
"last_login": {
"type": "string"
},
"role": {
"type": "string"
}
}
},
Expand Down
93 changes: 93 additions & 0 deletions docs/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,96 @@
}
}
},
"/profiles/{id}/pfp": {
"get": {
"description": "Returns an uploaded profile picture",
"consumes": [
"application/json"
],
"produces": [
"multipart/form-data"
],
"tags": [
"profiles"
],
"parameters": [
{
"type": "integer",
"description": "id of desired profile",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"404": {
"description": "record not found",
"schema": {
"$ref": "#/definitions/dtos.ErrResp"
}
},
"500": {
"description": "internal server error",
"schema": {
"$ref": "#/definitions/dtos.ErrResp"
}
}
}
},
"post": {
"description": "Uploads a profile picture",
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json"
],
"tags": [
"profiles"
],
"parameters": [
{
"type": "integer",
"description": "id of desired profile",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"201": {
"description": "returns newly created profile picture's URI",
"schema": {
"type": "string"
}
},
"400": {
"description": "no file sent",
"schema": {
"$ref": "#/definitions/dtos.ErrResp"
}
},
"401": {
"description": "unauthorized",
"schema": {
"$ref": "#/definitions/dtos.ErrResp"
}
},
"415": {
"description": "invalid media type",
"schema": {
"$ref": "#/definitions/dtos.ErrResp"
}
},
"500": {
"description": "internal server error",
"schema": {
"$ref": "#/definitions/dtos.ErrResp"
}
}
}
}
},
"/roles": {
"get": {
"description": "Reads all roles",
Expand Down Expand Up @@ -1877,6 +1967,9 @@
},
"last_login": {
"type": "string"
},
"role": {
"type": "string"
}
}
},
Expand Down
61 changes: 61 additions & 0 deletions docs/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ definitions:
type: boolean
last_login:
type: string
role:
type: string
type: object
dtos.UserUpdateDTO:
properties:
Expand Down Expand Up @@ -864,6 +866,65 @@ paths:
$ref: '#/definitions/dtos.ErrResp'
tags:
- profiles
/profiles/{id}/pfp:
get:
consumes:
- application/json
description: Returns an uploaded profile picture
parameters:
- description: id of desired profile
in: path
name: id
required: true
type: integer
produces:
- multipart/form-data
responses:
"404":
description: record not found
schema:
$ref: '#/definitions/dtos.ErrResp'
"500":
description: internal server error
schema:
$ref: '#/definitions/dtos.ErrResp'
tags:
- profiles
post:
consumes:
- multipart/form-data
description: Uploads a profile picture
parameters:
- description: id of desired profile
in: path
name: id
required: true
type: integer
produces:
- application/json
responses:
"201":
description: returns newly created profile picture's URI
schema:
type: string
"400":
description: no file sent
schema:
$ref: '#/definitions/dtos.ErrResp'
"401":
description: unauthorized
schema:
$ref: '#/definitions/dtos.ErrResp'
"415":
description: invalid media type
schema:
$ref: '#/definitions/dtos.ErrResp'
"500":
description: internal server error
schema:
$ref: '#/definitions/dtos.ErrResp'
tags:
- profiles
/roles:
get:
consumes:
Expand Down
95 changes: 95 additions & 0 deletions internal/DTOs/ItemDTOs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package dtos

import (
"context"
"smaash-web/internal/models"
"smaash-web/internal/utils"

"gorm.io/gorm"
)

type ItemReadDTO struct {
ID uint `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Price uint `json:"price"`
Rarity string `json:"rarity"`
Categories []string `json:"categories"`
}

type ItemCreateDTO struct {
Name string `json:"name" binding:"required,max=20"`
Description string `json:"description" binding:"required,max=50"`
Price uint `json:"price" binding:"required,gte=0"`
Rarity string `json:"rarity" binding:"required,max=9"`
Categories []string `json:"categories" binding:"required"`
}

type ItemUpdateDTO struct {
ID uint `json:"id" binding:"required"`
Name string `json:"name" binding:"required,max=20"`
Description string `json:"description" binding:"required,max=50"`
Price uint `json:"price" binding:"required,gte=0"`
Rarity string `json:"rarity" binding:"required.max=9"`
Categories []string `json:"categories" binding:"required"`
}

func ItemToDTO(item models.Item) ItemReadDTO {
return ItemReadDTO{
ID: item.ID,
Name: item.Name,
Description: item.Description,
Price: item.Price,
Rarity: item.Rarity.Name,
Categories: utils.Map(item.Categories, func(c *models.Category) string { return c.Name }),
}
}

func CreateDTOToItem(
dto ItemCreateDTO,
rarityExtracor func(c context.Context, rarityName string) (models.Rarity, error),
categoryExtractor func(c context.Context, categoryName string) (*models.Category, error),
) (*models.Item, error) {
rarity, err := rarityExtracor(context.Background(), dto.Rarity)
if err != nil {
return nil, err
}
return &models.Item{
Name: dto.Name,
Description: dto.Description,
Price: dto.Price,
RarityID: rarity.ID,
Categories: utils.Map(dto.Categories, func(name string) *models.Category {
category, err := categoryExtractor(context.Background(), name)
if err != nil {
panic("failed convert categories from request dto to model")
}
return category
}),
}, nil
}

func UpdateDTOToItem(
dto ItemUpdateDTO,
rarityExtractor func(c context.Context, rarityName string) (models.Rarity, error),
categoryExtractor func(c context.Context, categoryName string) (*models.Category, error),
) (*models.Item, error) {
rarity, err := rarityExtractor(context.Background(), dto.Rarity)
if err != nil {
return nil, err
}
return &models.Item{
Model: gorm.Model{ID: dto.ID},
Name: dto.Name,
Description: dto.Description,
Price: dto.Price,
RarityID: rarity.ID,
Categories: utils.Map(dto.Categories, func(name string) *models.Category {
category, err := categoryExtractor(context.Background(), name)
if err != nil {
panic("failed convert categories from request dto to model")
}
return category
}),
}, nil
}
Loading
Loading