Skip to content

Commit

Permalink
snake case dtos scenario publications
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
Pascal-Delange committed Sep 3, 2024
1 parent c2353cd commit 8d889b5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 39 deletions.
36 changes: 6 additions & 30 deletions api/handle_scenario_publications.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package api
import (
"fmt"
"net/http"
"time"

"github.com/cockroachdb/errors"
"github.com/gin-gonic/gin"
Expand All @@ -15,35 +14,15 @@ import (
"github.com/checkmarble/marble-backend/utils"
)

type APIScenarioPublication struct {
Id string `json:"id"`
Rank int32 `json:"rank"`
ScenarioId string `json:"scenarioID"`
ScenarioIterationId string `json:"scenarioIterationID"`
PublicationAction string `json:"publicationAction"`
CreatedAt time.Time `json:"createdAt"`
}

func NewAPIScenarioPublication(sp models.ScenarioPublication) APIScenarioPublication {
return APIScenarioPublication{
Id: sp.Id,
Rank: sp.Rank,
ScenarioId: sp.ScenarioId,
ScenarioIterationId: sp.ScenarioIterationId,
PublicationAction: sp.PublicationAction.String(),
CreatedAt: sp.CreatedAt,
}
}

func handleListScenarioPublications(uc usecases.Usecases) func(c *gin.Context) {
return func(c *gin.Context) {
organizationId, err := utils.OrganizationIdFromRequest(c.Request)
if presentError(c, err) {
return
}

scenarioID := c.Query("scenarioID")
scenarioIterationID := c.Query("scenarioIterationID")
scenarioID := c.Query("scenario_id")
scenarioIterationID := c.Query("scenario_iteration_id")

usecase := usecasesWithCreds(c.Request, uc).NewScenarioPublicationUsecase()
scenarioPublications, err := usecase.ListScenarioPublications(
Expand All @@ -56,7 +35,7 @@ func handleListScenarioPublications(uc usecases.Usecases) func(c *gin.Context) {
if presentError(c, err) {
return
}
c.JSON(http.StatusOK, pure_utils.Map(scenarioPublications, NewAPIScenarioPublication))
c.JSON(http.StatusOK, pure_utils.Map(scenarioPublications, dto.AdaptScenarioPublicationDto))
}
}

Expand All @@ -77,14 +56,11 @@ func handleCreateScenarioPublication(uc usecases.Usecases) func(c *gin.Context)
scenarioPublications, err := usecase.ExecuteScenarioPublicationAction(
c.Request.Context(),
organizationId,
models.PublishScenarioIterationInput{
ScenarioIterationId: data.ScenarioIterationId,
PublicationAction: models.PublicationActionFrom(data.PublicationAction),
})
dto.AdaptCreateScenarioPublicationBody(data))
if handleExpectedPublicationError(c, err) || presentError(c, err) {
return
}
c.JSON(http.StatusOK, pure_utils.Map(scenarioPublications, NewAPIScenarioPublication))
c.JSON(http.StatusOK, pure_utils.Map(scenarioPublications, dto.AdaptScenarioPublicationDto))
}
}

Expand All @@ -97,7 +73,7 @@ func handleGetScenarioPublication(uc usecases.Usecases) func(c *gin.Context) {
if presentError(c, err) {
return
}
c.JSON(http.StatusOK, NewAPIScenarioPublication(scenarioPublication))
c.JSON(http.StatusOK, dto.AdaptScenarioPublicationDto(scenarioPublication))
}
}

Expand Down
60 changes: 51 additions & 9 deletions dto/scenario_publications.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,61 @@
package dto

import "github.com/checkmarble/marble-backend/models"
import (
"time"

type CreateScenarioPublicationBody struct {
ScenarioIterationId string `json:"scenarioIterationID"`
PublicationAction string `json:"publicationAction"`
"github.com/checkmarble/marble-backend/models"
)

type ScenarioPublication struct {
Id string `json:"id"`
Rank int32 `json:"rank"`
ScenarioId_deprec string `json:"scenarioID"`
ScenarioId string `json:"scenario_id"`
ScenarioIterationId_deprec string `json:"scenarioIterationID"`
ScenarioIterationId string `json:"scenario_iteration_id"`
PublicationAction_deprec string `json:"publicationAction"`
PublicationAction string `json:"publication_action"`
CreatedAt_deprec time.Time `json:"createdAt"`
CreatedAt time.Time `json:"created_at"`
}

func AdaptScenarioPublicationDto(sp models.ScenarioPublication) ScenarioPublication {
return ScenarioPublication{
Id: sp.Id,
Rank: sp.Rank,
ScenarioId_deprec: sp.ScenarioId,
ScenarioId: sp.ScenarioId,
ScenarioIterationId_deprec: sp.ScenarioIterationId,
ScenarioIterationId: sp.ScenarioIterationId,
PublicationAction_deprec: sp.PublicationAction.String(),
PublicationAction: sp.PublicationAction.String(),
CreatedAt_deprec: sp.CreatedAt,
CreatedAt: sp.CreatedAt,
}
}

type CreateScenarioPublicationInput struct {
Body *CreateScenarioPublicationBody `in:"body=json"`
type CreateScenarioPublicationBody struct {
ScenarioIterationId_deprec string `json:"scenarioIterationID"`
ScenarioIterationId string `json:"scenario_iteration_id"`
PublicationAction_deprec string `json:"publicationAction"`
PublicationAction string `json:"publication_action"`
}

type ListScenarioPublicationsInput struct {
ScenarioId *string `in:"query=scenarioID"`
ScenarioIterationId *string `in:"query=scenarioIterationID"`
func AdaptCreateScenarioPublicationBody(dto CreateScenarioPublicationBody) models.PublishScenarioIterationInput {
out := models.PublishScenarioIterationInput{
ScenarioIterationId: dto.ScenarioIterationId,
PublicationAction: models.PublicationActionFrom(dto.PublicationAction),
}

// TODO remove deprecated fields
if out.ScenarioIterationId == "" {
out.ScenarioIterationId = dto.ScenarioIterationId_deprec
}
if out.PublicationAction == models.UnknownPublicationAction {
out.PublicationAction = models.PublicationActionFrom(dto.PublicationAction_deprec)
}

return out
}

type PublicationPreparationStatus struct {
Expand Down

0 comments on commit 8d889b5

Please sign in to comment.