Skip to content

Commit

Permalink
Merge pull request #58 from zbyju/dev
Browse files Browse the repository at this point in the history
Add support for issues and maintenances
  • Loading branch information
zbyju committed Jun 25, 2024
2 parents 3b01a3b + 44f892d commit d96bdcb
Show file tree
Hide file tree
Showing 50 changed files with 8,445 additions and 3,145 deletions.
2 changes: 1 addition & 1 deletion apps/babybox-service/Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM golang:1.22
WORKDIR /app

# Install air for live reloading
RUN go install github.com/cosmtrek/air@latest
RUN go install github.com/air-verse/air@latest

# Copy go mod and sum files
COPY go.mod go.sum ./
Expand Down
5 changes: 4 additions & 1 deletion apps/babybox-service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/zbyju/babybox-dashboard/apps/babybox-service

go 1.22.0

require golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect
require (
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569 // indirect
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect
)

require (
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
Expand Down
2 changes: 2 additions & 0 deletions apps/babybox-service/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ github.com/streadway/amqp v1.1.0 h1:py12iX8XSyI7aN/3dUT8DFIDJazNJsVJdxNVEpnQTZM=
github.com/streadway/amqp v1.1.0/go.mod h1:WYSrTEYHOXHd0nwFeUXAe2G2hRnQT+deZJJf88uS9Bg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569 h1:xzABM9let0HLLqFypcxvLmlvEciCHL7+Lv+4vwZqecI=
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569/go.mod h1:2Ly+NIftZN4de9zRmENdYbvPQeaVIYKWpLFStLFEBgI=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
Expand Down
118 changes: 118 additions & 0 deletions apps/babybox-service/internal/api/v1/issues.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package v1

import (
"fmt"
"net/http"
"time"

"github.com/labstack/echo/v4"
"github.com/zbyju/babybox-dashboard/apps/babybox-service/internal/domain"
"go.mongodb.org/mongo-driver/mongo"
)

// CreateIssue inserts a new BabyboxIssue
func (app *Application) CreateIssue(c echo.Context) error {
issue := new(domain.BabyboxIssue)
if err := c.Bind(issue); err != nil {
return err
}

now := time.Now().In(app.DBService.TimeLocation)
issue.CreatedAt = &now

result, err := app.DBService.InsertIssue(*issue)
if err != nil {
return c.JSON(http.StatusInternalServerError, ReturnErr(err))
}

return c.JSON(http.StatusCreated, ReturnOk(result))
}

// UpdateIssue updates an existing BabyboxIssue
func (app *Application) UpdateIssue(c echo.Context) error {
idStr := c.Param("id")

issue := new(domain.BabyboxIssue)
if err := c.Bind(issue); err != nil {
return err
}

updatedIssue, err := app.DBService.UpdateIssue(*issue)
if err != nil {
if err == mongo.ErrNoDocuments {
return c.JSON(http.StatusNotFound, ReturnErr(fmt.Sprintf("Issue with ID: '%s' was not found.", idStr)))
}
return c.JSON(http.StatusInternalServerError, ReturnErr(err))
}

return c.JSON(http.StatusOK, ReturnOk(updatedIssue))
}

// DeleteIssue deletes a BabyboxIssue by its ID
func (app *Application) DeleteIssue(c echo.Context) error {
id := c.Param("id")

err := app.DBService.DeleteIssue(id)
if err != nil {
if err == mongo.ErrNoDocuments {
return c.JSON(http.StatusNotFound, ReturnErr(fmt.Sprintf("Issue with ID: '%s' was not found.", id)))
}
return c.JSON(http.StatusInternalServerError, ReturnErr(err))
}

return c.NoContent(http.StatusNoContent)
}

// GetAllIssues gets all BabyboxIssues
func (app *Application) GetAllIssues(c echo.Context) error {
issues, err := app.DBService.FindAllIssues()
if err != nil {
return c.JSON(http.StatusInternalServerError, ReturnErr(err))
}

return c.JSON(http.StatusOK, ReturnOk(issues))
}

// GetIssuesBySlug finds all BabyboxIssues with a specific slug
func (app *Application) GetIssuesBySlug(c echo.Context) error {
slug := c.Param("slug")

issues, err := app.DBService.FindIssuesBySlug(slug)
if err != nil {
return c.JSON(http.StatusInternalServerError, ReturnErr(err))
}

return c.JSON(http.StatusOK, ReturnOk(issues))
}

// GetIssuesByUsername finds all BabyboxIssues assigned to a specific user
func (app *Application) GetIssuesByUsername(c echo.Context) error {
username := c.Param("username")

issues, err := app.DBService.FindIssuesByUsername(username)
if err != nil {
return c.JSON(http.StatusInternalServerError, ReturnErr(err))
}

return c.JSON(http.StatusOK, ReturnOk(issues))
}

func (app *Application) GetIssuesUnsolved(c echo.Context) error {
issues, err := app.DBService.FindMaintenancesUnsolvedOptionallyBySlug("")
if err != nil {
return c.JSON(http.StatusInternalServerError, ReturnErr(err))
}

return c.JSON(http.StatusOK, ReturnOk(issues))
}

func (app *Application) GetIssuesUnsolvedBySlug(c echo.Context) error {
slug := c.Param("slug")

issues, err := app.DBService.FindMaintenancesUnsolvedOptionallyBySlug(slug)
if err != nil {
return c.JSON(http.StatusInternalServerError, ReturnErr(err))
}

return c.JSON(http.StatusOK, ReturnOk(issues))
}
82 changes: 82 additions & 0 deletions apps/babybox-service/internal/api/v1/maintenances.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package v1

import (
"fmt"
"net/http"

"github.com/labstack/echo/v4"
"github.com/zbyju/babybox-dashboard/apps/babybox-service/internal/domain"
"go.mongodb.org/mongo-driver/mongo"
)

// CreateMaintenance inserts a new BabyboxMaintenance
func (app *Application) CreateMaintenance(c echo.Context) error {
maintenance := new(domain.BabyboxMaintenance)
if err := c.Bind(maintenance); err != nil {
return err
}

result, err := app.DBService.InsertMaintenance(*maintenance)
if err != nil {
return c.JSON(http.StatusInternalServerError, ReturnErr(err))
}

return c.JSON(http.StatusCreated, ReturnOk(result))
}

// UpdateMaintenance updates an existing BabyboxMaintenance
func (app *Application) UpdateMaintenance(c echo.Context) error {
idStr := c.Param("id")

maintenance := new(domain.BabyboxMaintenance)
if err := c.Bind(maintenance); err != nil {
return err
}

updatedMaintenance, err := app.DBService.UpdateMaintenance(*maintenance)
if err != nil {
if err == mongo.ErrNoDocuments {
return c.JSON(http.StatusNotFound, ReturnErr(fmt.Sprintf("Maintenance with ID: '%s' was not found.", idStr)))
}
return c.JSON(http.StatusInternalServerError, ReturnErr(err))
}

return c.JSON(http.StatusOK, ReturnOk(updatedMaintenance))
}

// DeleteMaintenance deletes a BabyboxMaintenance by its ID
func (app *Application) DeleteMaintenance(c echo.Context) error {
id := c.Param("id")

err := app.DBService.DeleteMaintenance(id, false)
if err != nil {
if err == mongo.ErrNoDocuments {
return c.JSON(http.StatusNotFound, ReturnErr(fmt.Sprintf("Maintenance with ID: '%s' was not found.", id)))
}
return c.JSON(http.StatusInternalServerError, ReturnErr(err))
}

return c.NoContent(http.StatusNoContent)
}

// GetAllMaintenances gets all BabyboxMaintenances
func (app *Application) GetAllMaintenances(c echo.Context) error {
maintenances, err := app.DBService.FindAllMaintenances()
if err != nil {
return c.JSON(http.StatusInternalServerError, ReturnErr(err))
}

return c.JSON(http.StatusOK, ReturnOk(maintenances))
}

// GetMaintenancesBySlug finds all BabyboxMaintenances with a specific slug
func (app *Application) GetMaintenancesBySlug(c echo.Context) error {
slug := c.Param("slug")

maintenances, err := app.DBService.FindMaintenancesBySlug(slug)
if err != nil {
return c.JSON(http.StatusInternalServerError, ReturnErr(err))
}

return c.JSON(http.StatusOK, ReturnOk(maintenances))
}
12 changes: 12 additions & 0 deletions apps/babybox-service/internal/api/v1/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,16 @@ func RegisterRoutes(g *echo.Group, app *Application) {
g.GET("/babyboxes", app.GetAllBabyboxesNames)
g.GET("/babyboxes/:slug", app.GetBabyboxBySlug)
g.PUT("/babyboxes/:slug", app.UpdateBabybox)

g.POST("/issues", app.CreateIssue)
g.GET("/issues", app.GetAllIssues)
g.GET("/issues/unsolved", app.GetIssuesUnsolved)
g.GET("/issues/unsolved/:slug", app.GetIssuesUnsolvedBySlug)
g.GET("/issues/username/:username", app.GetIssuesByUsername)
g.GET("/issues/:slug", app.GetIssuesBySlug)
g.PUT("/issues/:id", app.UpdateIssue)

g.POST("/maintenances", app.CreateMaintenance)
g.GET("/maintenances", app.GetAllMaintenances)
g.GET("/maintenances/:slug", app.GetMaintenancesBySlug)
}
Loading

0 comments on commit d96bdcb

Please sign in to comment.