Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First deliverable #6

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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 .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CSVFile=csv/input_file.csv
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
100 changes: 100 additions & 0 deletions business/mocks/pokemon.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions business/pokemon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package business

import (
"log"

"github.com/rmonroy-wiz/ondemand-go-bootcamp-2022/model"
"github.com/rmonroy-wiz/ondemand-go-bootcamp-2022/repository"
"github.com/rmonroy-wiz/ondemand-go-bootcamp-2022/service"
)

//go:generate mockery --name PokemonBusiness --filename pokemon.go --outpkg mocks --structname PokemonBusinessMock --disable-version-string
type PokemonBusiness interface {
GetAll() ([]*model.PokemonDTO, *model.ErrorHandler)
GetByID(id int) (*model.PokemonDTO, *model.ErrorHandler)
StoreByID(id int) (*model.PokemonDTO, *model.ErrorHandler)
}

// PokemonService dependencies from Pokemon service
type pokemonBusiness struct {
pokemonRepository repository.PokemonRepository
serviceAPI service.ExternalPokemonAPI
}

// NewPokemonService initializer method for create PokemonService
func NewPokemonBusiness(repository repository.PokemonRepository, service service.ExternalPokemonAPI) *pokemonBusiness {
return &pokemonBusiness{
pokemonRepository: repository,
serviceAPI: service,
}
}

// GetAll get all pokemons from repository
func (s pokemonBusiness) GetAll() ([]*model.PokemonDTO, *model.ErrorHandler) {
log.Println("enter to get all pokemons!!!")
pokemons, err := s.pokemonRepository.GetAll()
if err != nil {
return nil, err
}
return pokemons, nil
}

// GetByID get pokemon by his id
func (s pokemonBusiness) GetByID(id int) (*model.PokemonDTO, *model.ErrorHandler) {
log.Println("enter to get pokemon by id!!!")
pokemon, err := s.pokemonRepository.GetByID(id)
if err != nil {
return nil, err
}
return pokemon, nil
}

// StoreByID get pokemon by his id
func (s pokemonBusiness) StoreByID(id int) (*model.PokemonDTO, *model.ErrorHandler) {
log.Println("enter to search and store pokemon by id!!!")
pokemonAPI, err := s.serviceAPI.GetPokemonFromAPI(id)
if err != nil {
return nil, err
}
pokemon, errRepository := s.pokemonRepository.StoreToCSV(*pokemonAPI)
if err != nil {
return nil, errRepository
}
return pokemon, nil
}
21 changes: 21 additions & 0 deletions controller/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package controller

import (
"github.com/gin-gonic/gin"
"github.com/rmonroy-wiz/ondemand-go-bootcamp-2022/model"
)

type baseController struct {
}

func (ctrl baseController) ResponseError(c *gin.Context, err *model.ErrorHandler) {
c.JSON(err.StatusCode, gin.H{
"errors": err,
})
}

func (ctrl baseController) ResponseSucess(c *gin.Context, statusCode int, object interface{}) {
c.JSON(statusCode, gin.H{
"data": object,
})
}
64 changes: 64 additions & 0 deletions controller/pokemon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package controller

import (
"net/http"
"strconv"

"github.com/gin-gonic/gin"
"github.com/rmonroy-wiz/ondemand-go-bootcamp-2022/business"
"github.com/rmonroy-wiz/ondemand-go-bootcamp-2022/model"
)

type pokemon struct {
pokemonBusiness business.PokemonBusiness
baseController
}

func NewPokemonController(pokemonBusiness business.PokemonBusiness) *pokemon {
return &pokemon{
pokemonBusiness: pokemonBusiness,
}
}

// Get all pokemons
func (ctrl pokemon) GetAllPokemons(c *gin.Context) {
pokemons, err := ctrl.pokemonBusiness.GetAll()

if err != nil {
ctrl.ResponseError(c, err)
}

ctrl.ResponseSucess(c, http.StatusOK, pokemons)
}

// GetPokemonByID get pokemon based on ID
func (ctrl pokemon) GetPokemonByID(c *gin.Context) {
id, err := strconv.Atoi(c.Params.ByName("pokemonId"))
if err != nil {
ctrl.ResponseError(c, model.NewURLParameterDoesNotFound("pokemonId"))
return
}
pokemon, errBusiness := ctrl.pokemonBusiness.GetByID(id)
if errBusiness != nil {
ctrl.ResponseError(c, errBusiness)
return
}

ctrl.ResponseSucess(c, http.StatusOK, pokemon)
}

// StorePokemonByID get pokemon based on ID
func (ctrl pokemon) StorePokemonByID(c *gin.Context) {
id, err := strconv.Atoi(c.Params.ByName("pokemonId"))
if err != nil {
ctrl.ResponseError(c, model.NewURLParameterDoesNotFound("pokemonId"))
return
}
pokemon, errBusiness := ctrl.pokemonBusiness.StoreByID(id)
if errBusiness != nil {
ctrl.ResponseError(c, errBusiness)
return
}

ctrl.ResponseSucess(c, http.StatusCreated, pokemon)
}
Loading