Skip to content

Commit

Permalink
feat: (api) Evolution models and controllers
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Potter <pttr.jcb@gmail.com>
  • Loading branch information
JacobPotter committed Nov 6, 2024
1 parent a58fdfc commit 418b44c
Show file tree
Hide file tree
Showing 43 changed files with 106,367 additions and 455 deletions.
24 changes: 21 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,35 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Install PostgreSQL client
run: |
apt-get update
apt-get install --yes postgresql-client
- name: Postgres Backup Restore
uses: tj-actions/pg-restore@v6
with:
database_url: "postgres://postgres:postgres@postgres:5432/postgres"
backup_file: "src/seed/mock_dump.sql"

- name: Build
run: go build -v -buildvcs=false ./...

- name: Test
- name: Test Jobs
env:
DB_HOST: postgres
DB_NAME: postgres
DB_USER: postgres
DB_PASSWORD: postgres
run: go test github.com/WebWizardsDev/poke-db/api/jobs -v ./...

- name: Test Controllers
env:
DB_HOST: postgres
DB_NAME: postgres
DB_USER: postgres
DB_PASSWORD: postgres
INIT_JOBS: true
run: go test -v ./...
run: go test github.com/WebWizardsDev/poke-db/api/controllers -v ./...

build-frontend:
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions api/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ go.work

.env
*.env

.idea
8 changes: 0 additions & 8 deletions api/.idea/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion api/.idea/.name

This file was deleted.

18 changes: 0 additions & 18 deletions api/.idea/dataSources.xml

This file was deleted.

12 changes: 0 additions & 12 deletions api/.idea/material_theme_project_new.xml

This file was deleted.

7 changes: 0 additions & 7 deletions api/.idea/misc.xml

This file was deleted.

11 changes: 0 additions & 11 deletions api/.idea/modules.xml

This file was deleted.

12 changes: 0 additions & 12 deletions api/.idea/poke-db-api.iml

This file was deleted.

12 changes: 0 additions & 12 deletions api/.idea/rcb-settings.xml

This file was deleted.

7 changes: 0 additions & 7 deletions api/.idea/vcs.xml

This file was deleted.

54 changes: 54 additions & 0 deletions api/controllers/evolution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package controllers

import (
"github.com/WebWizardsDev/poke-db/api/models"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"net/http"
)

type EvolutionHandler struct {
db *gorm.DB
}

func NewEvolutionHandler(db *gorm.DB) *EvolutionHandler {
return &EvolutionHandler{db: db}
}

func (e *EvolutionHandler) GetEvolutionChain(c *gin.Context) {
id := c.Param("id")
var evolutionChain models.EvolutionChain
if err := e.db.
Preload(clause.Associations).
Preload("ChainLink."+clause.Associations).
Preload("ChainLink.EvolvesTo."+clause.Associations).
Preload("ChainLink.EvolvesTo.EvolvesTo."+clause.Associations).
Preload("ChainLink.EvolvesTo.EvolvesTo.EvolvesTo."+clause.Associations).
First(&evolutionChain, id).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Evolution Chain not found"})
return
}
c.JSON(http.StatusOK, evolutionChain)
}

func (e *EvolutionHandler) ListEvolutionChains(c *gin.Context) {

var evolutionChains []models.EvolutionChain

if tx := e.db.
Preload(clause.Associations).
Preload("ChainLink." + clause.Associations).
Preload("ChainLink.EvolvesTo." + clause.Associations).
Preload("ChainLink.EvolvesTo.EvolutionDetails." + clause.Associations).
Preload("ChainLink.EvolvesTo.EvolvesTo." + clause.Associations).
Preload("ChainLink.EvolvesTo.EvolvesTo.EvolutionDetails." + clause.Associations).
Preload("ChainLink.EvolvesTo.EvolvesTo.EvolvesTo." + clause.Associations).
Preload("ChainLink.EvolvesTo.EvolvesTo.EvolvesTo.EvolutionDetails." + clause.Associations).
Find(&evolutionChains); tx.Error != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error while listing evolution chains"})
return
}
c.JSON(http.StatusOK, gin.H{"evolutionChains": evolutionChains})

}
26 changes: 13 additions & 13 deletions api/controllers/pokemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package controllers

import (
"fmt"
"github.com/JacobPotter/poke-db/api/models"
"github.com/WebWizardsDev/poke-db/api/models"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"gorm.io/gorm/clause"
Expand Down Expand Up @@ -66,20 +66,20 @@ func NewPokemonHandler(db *gorm.DB) *PokemonHandler {
// c.JSON(http.StatusCreated, pokemon)
//}

// GetPokemon retrieves the details of a Pokemon based on the given ID.
// It first retrieves the Pokemon from the database using the provided ID.
// GetPokemon retrieves the details of a Pokemon based on the given GenericId.
// It first retrieves the Pokemon from the database using the provided GenericId.
// If the Pokemon is not found, it returns a 404 Not Found response.
// If the Pokemon is found, it returns a 200 OK response with the Pokemon data.
//
// @Summary Get Pokemon
//
// @Description Get pokemon by ID
// @Description Get pokemon by GenericId
//
// @Tags pokemon
//
// @Accept json
//
// @Param id path int true "Pokemon ID"
// @Param id path int true "Pokemon GenericId"
//
// @Produce json
//
Expand Down Expand Up @@ -187,23 +187,23 @@ func (h *PokemonHandler) ListPokemon(c *gin.Context) {
})
}

// UpdatePokemon updates the details of a Pokemon based on the provided ID.
// It first retrieves the Pokemon from the database using the provided ID.
// UpdatePokemon updates the details of a Pokemon based on the provided GenericId.
// It first retrieves the Pokemon from the database using the provided GenericId.
// If the Pokemon is not found, it returns a 404 Not Found response with an error message.
// If the JSON payload is invalid, it returns a 400 Bad Request response with the error details.
// If the update is successful, it updates the Pokemon in the database and returns a 200 OK response with the updated Pokemon data.
//
// @Summary Update Pokemon
//
// @Description Update pokemon by ID
// @Description Update pokemon by GenericId
//
// @Tags pokemon
//
// @Accept json
//
// @Produce json
//
// @Param id path int true "Pokemon ID"
// @Param id path int true "Pokemon GenericId"
//
// @Param data body models.Pokemon true "The updated pokemon"
//
Expand All @@ -228,23 +228,23 @@ func (h *PokemonHandler) ListPokemon(c *gin.Context) {
// c.JSON(http.StatusOK, updatedPokemon)
//}

// DeletePokemon deletes a Pokemon from the database based on the provided ID.
// It first retrieves the Pokemon from the database using the provided ID.
// DeletePokemon deletes a Pokemon from the database based on the provided GenericId.
// It first retrieves the Pokemon from the database using the provided GenericId.
// If the Pokemon is not found, it returns a 404 Not Found response with an error message.
// If the delete operation encounters an error, it returns a 500 Internal Server Error response with an error message.
// If the delete operation is successful, it returns a 200 OK response with a success message.
//
// @Summary Delete Pokemon
//
// @Description Delete pokemon by ID
// @Description Delete pokemon by GenericId
//
// @Tags pokemon
//
// @Accept json
//
// @Produce json
//
// @Param id path int true "Pokemon ID"
// @Param id path int true "Pokemon GenericId"
//
// @Success 200 {string} string "deleted"
//
Expand Down
12 changes: 2 additions & 10 deletions api/controllers/pokemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package controllers_test

import (
"encoding/json"
"github.com/JacobPotter/poke-db/api/jobs"
"github.com/JacobPotter/poke-db/api/models"
"github.com/JacobPotter/poke-db/api/routes"
"github.com/WebWizardsDev/poke-db/api/models"
"github.com/WebWizardsDev/poke-db/api/routes"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/suite"
"net/http"
"net/http/httptest"
"os"
"testing"
)

Expand All @@ -23,12 +21,6 @@ func (s *PokmemonTestSuite) SetupTest() {
models.ConnectDatabase()
s.router = routes.SetupRouter(models.DB.Debug())

initJobs := os.Getenv("INIT_JOBS")

if initJobs == "true" {
jobs.RefreshDB{DB: models.DB, Test: true}.Run()
}

}

func TestPokemonSuite(t *testing.T) {
Expand Down
Loading

0 comments on commit 418b44c

Please sign in to comment.