Skip to content

Commit

Permalink
fix: Database level relantionship between associations
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricioabreu committed Apr 23, 2024
1 parent eed7295 commit 5e01d2a
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 128 deletions.
60 changes: 27 additions & 33 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const docTemplate = `{
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/model.Input"
"$ref": "#/definitions/api.InputData"
}
}
],
Expand Down Expand Up @@ -172,6 +172,17 @@ const docTemplate = `{
}
},
"definitions": {
"api.AudioProfileData": {
"type": "object",
"properties": {
"bitrate": {
"type": "integer"
},
"codec": {
"type": "string"
}
}
},
"api.Error": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -205,40 +216,17 @@ const docTemplate = `{
}
}
},
"api.SuccessResponse": {
"type": "object",
"properties": {
"data": {},
"message": {
"type": "string"
}
}
},
"model.AudioProfile": {
"type": "object",
"properties": {
"codec": {
"type": "string"
},
"inputID": {
"type": "string"
},
"rate": {
"type": "integer"
}
}
},
"model.Input": {
"api.InputData": {
"type": "object",
"required": [
"format",
"name"
],
"properties": {
"audio": {
"audio_profiles": {
"type": "array",
"items": {
"$ref": "#/definitions/model.AudioProfile"
"$ref": "#/definitions/api.AudioProfileData"
}
},
"format": {
Expand All @@ -247,15 +235,24 @@ const docTemplate = `{
"name": {
"type": "string"
},
"video": {
"video_profiles": {
"type": "array",
"items": {
"$ref": "#/definitions/model.VideoProfile"
"$ref": "#/definitions/api.VideoProfileData"
}
}
}
},
"model.VideoProfile": {
"api.SuccessResponse": {
"type": "object",
"properties": {
"data": {},
"message": {
"type": "string"
}
}
},
"api.VideoProfileData": {
"type": "object",
"properties": {
"bitrate": {
Expand All @@ -270,9 +267,6 @@ const docTemplate = `{
"height": {
"type": "integer"
},
"inputID": {
"type": "string"
},
"max_key_interval": {
"type": "integer"
},
Expand Down
60 changes: 27 additions & 33 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/model.Input"
"$ref": "#/definitions/api.InputData"
}
}
],
Expand Down Expand Up @@ -165,6 +165,17 @@
}
},
"definitions": {
"api.AudioProfileData": {
"type": "object",
"properties": {
"bitrate": {
"type": "integer"
},
"codec": {
"type": "string"
}
}
},
"api.Error": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -198,40 +209,17 @@
}
}
},
"api.SuccessResponse": {
"type": "object",
"properties": {
"data": {},
"message": {
"type": "string"
}
}
},
"model.AudioProfile": {
"type": "object",
"properties": {
"codec": {
"type": "string"
},
"inputID": {
"type": "string"
},
"rate": {
"type": "integer"
}
}
},
"model.Input": {
"api.InputData": {
"type": "object",
"required": [
"format",
"name"
],
"properties": {
"audio": {
"audio_profiles": {
"type": "array",
"items": {
"$ref": "#/definitions/model.AudioProfile"
"$ref": "#/definitions/api.AudioProfileData"
}
},
"format": {
Expand All @@ -240,15 +228,24 @@
"name": {
"type": "string"
},
"video": {
"video_profiles": {
"type": "array",
"items": {
"$ref": "#/definitions/model.VideoProfile"
"$ref": "#/definitions/api.VideoProfileData"
}
}
}
},
"model.VideoProfile": {
"api.SuccessResponse": {
"type": "object",
"properties": {
"data": {},
"message": {
"type": "string"
}
}
},
"api.VideoProfileData": {
"type": "object",
"properties": {
"bitrate": {
Expand All @@ -263,9 +260,6 @@
"height": {
"type": "integer"
},
"inputID": {
"type": "string"
},
"max_key_interval": {
"type": "integer"
},
Expand Down
44 changes: 20 additions & 24 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
basePath: /api/v1
definitions:
api.AudioProfileData:
properties:
bitrate:
type: integer
codec:
type: string
type: object
api.Error:
properties:
details:
Expand All @@ -21,40 +28,31 @@ definitions:
error:
$ref: '#/definitions/api.Error'
type: object
api.SuccessResponse:
api.InputData:
properties:
data: {}
message:
type: string
type: object
model.AudioProfile:
properties:
codec:
type: string
inputID:
type: string
rate:
type: integer
type: object
model.Input:
properties:
audio:
audio_profiles:
items:
$ref: '#/definitions/model.AudioProfile'
$ref: '#/definitions/api.AudioProfileData'
type: array
format:
type: string
name:
type: string
video:
video_profiles:
items:
$ref: '#/definitions/model.VideoProfile'
$ref: '#/definitions/api.VideoProfileData'
type: array
required:
- format
- name
type: object
model.VideoProfile:
api.SuccessResponse:
properties:
data: {}
message:
type: string
type: object
api.VideoProfileData:
properties:
bitrate:
type: integer
Expand All @@ -64,8 +62,6 @@ definitions:
type: integer
height:
type: integer
inputID:
type: string
max_key_interval:
type: integer
width:
Expand All @@ -88,7 +84,7 @@ paths:
name: input
required: true
schema:
$ref: '#/definitions/model.Input'
$ref: '#/definitions/api.InputData'
produces:
- application/json
responses:
Expand Down
40 changes: 5 additions & 35 deletions internal/api/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewInputController(inputHandler service.InputHandler) *InputController {
// @Description Create an input ready to be ingested
// @Accept json
// @Produce json
// @Param input body model.Input true "Input data"
// @Param input body api.InputData true "Input data"
// @Success 201 {object} api.SuccessResponse
// @Failure 400 {object} api.ErrorResponse "Invalid input data"
// @Failure 500 {object} api.ErrorResponse "Internal server error"
Expand All @@ -46,32 +46,7 @@ func (c *InputController) CreateInput(ctx *gin.Context) {
return
}

audioProfiles := make([]service.AudioProfile, 0, len(inputData.AudioProfiles))
for _, audioProfileData := range inputData.AudioProfiles {
audioProfiles = append(audioProfiles, service.AudioProfile{
Codec: audioProfileData.Codec,
Bitrate: audioProfileData.Bitrate,
})
}

videoProfiles := make([]service.VideoProfile, 0, len(inputData.VideoProfiles))
for _, videoProfileData := range inputData.VideoProfiles {
videoProfiles = append(videoProfiles, service.VideoProfile{
Codec: videoProfileData.Codec,
Bitrate: videoProfileData.Bitrate,
MaxKeyInterval: videoProfileData.MaxKeyInterval,
Framerate: videoProfileData.Framerate,
Width: videoProfileData.Width,
Height: videoProfileData.Height,
})
}

input, err := c.inputHandler.CreateInput(ctx, &service.Input{
Name: inputData.Name,
Format: inputData.Format,
AudioProfiles: audioProfiles,
VideoProfiles: videoProfiles,
})
input, err := c.inputHandler.CreateInput(ctx, inputData.ToInput())
if err != nil {
ctx.JSON(http.StatusInternalServerError, ErrorResponse{
Error: Error{Message: "InternalServerError: failed creating input"},
Expand All @@ -80,13 +55,7 @@ func (c *InputController) CreateInput(ctx *gin.Context) {
return
}

responseData := InputData{
ID: input.ID,
Name: input.Name,
Format: input.Format,
AudioProfiles: inputData.AudioProfiles,
VideoProfiles: inputData.VideoProfiles,
}
responseData := FromInput(input)
ctx.JSON(http.StatusCreated, SuccessResponse{
Message: "Input created successfully",
Data: responseData,
Expand Down Expand Up @@ -120,8 +89,9 @@ func (c *InputController) GetInput(ctx *gin.Context) {
return
}

responseData := FromInput(input)
ctx.JSON(http.StatusOK, SuccessResponse{
Data: input,
Data: responseData,
})
}

Expand Down
Loading

0 comments on commit 5e01d2a

Please sign in to comment.