Skip to content

Commit

Permalink
Merge pull request #7 from agomezjuan/2-administración-de-datos-de-lo…
Browse files Browse the repository at this point in the history
…s-paciente

2 administración de datos de los paciente
  • Loading branch information
agomezjuan committed Mar 28, 2024
2 parents 5cf3b5d + b4eef47 commit e566a1b
Show file tree
Hide file tree
Showing 15 changed files with 781 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Build the Go app
run: |
cd cmd/server
go build -v -o api
go build -v -o ../../out
- name: Deploy to Railway
run: railway up --service ${{ secrets.RAILWAY_SERVICE }}
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,5 @@ $RECYCLE.BIN/
# Windows shortcuts
*.lnk

# End of https://www.toptal.com/developers/gitignore/api/go,goland+all,windows,macos,visualstudiocode
# End of https://www.toptal.com/developers/gitignore/api/go,goland+all,windows,macos,visualstudiocode
data
189 changes: 189 additions & 0 deletions cmd/server/handler/dentista.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package handler

import (
"errors"
"os"
"strconv"

"github.com/agomezjuan/desafio-integrador-go-ii/internal/dentista"
"github.com/agomezjuan/desafio-integrador-go-ii/internal/domain"
"github.com/agomezjuan/desafio-integrador-go-ii/pkg/web"
"github.com/gin-gonic/gin"
)

type DentistaHandler struct {
s dentista.Service
}

func NewDentistaHandler(s dentista.Service) *DentistaHandler {
return &DentistaHandler{s: s}
}

// GetByID godoc
// @Summary List dentists
// @Tags Dentista
// @Description get dentista
// @Produce json
// @Success 200 {object} web.response
// @Router /dentistas [get]
func (h *DentistaHandler) GetByID() gin.HandlerFunc {
return func(c *gin.Context) {
idParam := c.Param("id")
id, err := strconv.Atoi(idParam)
if err != nil {
web.Failure(c, 400, errors.New("id inválido"))
return
}
dentista, err := h.s.GetByID(id)
if err != nil {
web.Failure(c, 404, errors.New("dentista no encontrado"))
return
}
web.Success(c, 200, dentista)
}
}

// CreateDentista godoc
// @Summary Crear dentista
// @Tags Dentista
// @Description create dentista
// @Accept json
// @Produce json
// @Param token header string true "token"
// @Success 200 {object} web.response
// @Router /dentistas [post]
func (h *DentistaHandler) Create() gin.HandlerFunc {
return func(c *gin.Context) {
var d domain.Dentista
if err := c.ShouldBindJSON(&d); err != nil {
web.Failure(c, 400, errors.New("datos inválidos"))
return
}
dentista, err := h.s.Create(d)
if err != nil {
web.Failure(c, 400, err)
return
}
web.Success(c, 201, dentista)
}
}

// UpdateDentista godoc
// @Summary Actualizar dentista
// @Tags Dentista
// @Description update dentista
// @Accept json
// @Produce json
// @Param token header string true "token"
// @Success 200 {object} web.response
// @Router /dentistas/{id} [put]
func (h *DentistaHandler) Update() gin.HandlerFunc {
return func(c *gin.Context) {
idParam := c.Param("id")
id, err := strconv.Atoi(idParam)
if err != nil {
web.Failure(c, 400, errors.New("id inválido"))
return
}
var d domain.Dentista
if err := c.ShouldBindJSON(&d); err != nil {
web.Failure(c, 400, errors.New("datos inválidos"))
return
}
dentista, err := h.s.Update(id, d)
if err != nil {
web.Failure(c, 400, err)
return
}
web.Success(c, 200, dentista)
}
}

// UpdateByField godoc
// @Summary Actualizar dentista por alguno de sus campos
// @Tags Dentista
// @Description update dentista
// @Accept json
// @Produce json
// @Param token header string true "token"
// @Success 200 {object} web.response
// @Router /dentistas/{id} [patch]
func (h *DentistaHandler) UpdateByField() gin.HandlerFunc {
return func(c *gin.Context) {
token := c.GetHeader("TOKEN")
if token == "" {
web.Failure(c, 401, errors.New("token not found"))
return
}
if token != os.Getenv("TOKEN") {
web.Failure(c, 401, errors.New("invalid token"))
return
}
idParam := c.Param("id")
id, err := strconv.Atoi(idParam)
if err != nil {
web.Failure(c, 400, errors.New("id inválido"))
return
}
exists, err := h.s.GetByID(id)
if err != nil {
web.Failure(c, 500, err)
return
}
if exists.Id == 0 {
web.NotFound(c)
return
}
var d domain.Dentista
if err := c.ShouldBindJSON(&d); err != nil {
web.Failure(c, 400, errors.New(err.Error()))
return
}

d.Id = exists.Id

if d.Apellido == "" {
d.Apellido = exists.Apellido
}

if d.Nombre == "" {
d.Nombre = exists.Nombre
}

if d.Matricula == "" {
d.Matricula = exists.Matricula
}

dentista, err := h.s.Update(id, d)
if err != nil {
web.Failure(c, 500, err)
return
}
web.Success(c, 200, dentista)

}
}

// DeleteDentista godoc
// @Summary Eliminar dentista
// @Tags Dentista
// @Description delete dentista
// @Param token header string true "token"
// @Success 204 {object} web.response
// @Router /dentistas/{id} [delete]
func (h *DentistaHandler) Delete() gin.HandlerFunc {
return func(c *gin.Context) {
idParam := c.Param("id")
id, err := strconv.Atoi(idParam)
if err != nil {
web.Failure(c, 400, errors.New("id inválido"))
return
}
err = h.s.Delete(id)
if err != nil {
web.Failure(c, 400, err)
return
}
web.Success(c, 204, nil)
}
}
32 changes: 29 additions & 3 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,47 @@
package main

import (
"database/sql"
"log"

"github.com/agomezjuan/desafio-integrador-go-ii/cmd/server/handler"
"github.com/agomezjuan/desafio-integrador-go-ii/internal/dentista"
"github.com/agomezjuan/desafio-integrador-go-ii/pkg/store"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
)

func main() {

bd, err := sql.Open("mysql", "root:root@tcp(localhost:3346)/my_db")
if err != nil {
log.Fatal(err)
}

// Creamos el servicio
storage := store.NewSqlStore(bd)
repo := dentista.NewRepository(storage)
service := dentista.NewService(repo)
dentistaHandler := handler.NewDentistaHandler(service)

// Creamos el router
router := gin.Default()
r := gin.Default()

// Definimos una ruta para consultar el estado del servidor
router.GET("/ping", func(c *gin.Context) {
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})

// Definimos una ruta para consultar un dentista por su id
dentistas := r.Group("/dentistas")
{
dentistas.POST("/", dentistaHandler.Create())
dentistas.GET("/:id", dentistaHandler.GetByID())
dentistas.PUT("/:id", dentistaHandler.Update())
}

// Ejecutamos el servidor en el puerto 8081
router.Run(":8081")
r.Run(":8081")
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ module github.com/agomezjuan/desafio-integrador-go-ii
go 1.21.6

require (
github.com/gin-gonic/gin v1.9.1
github.com/go-sql-driver/mysql v1.8.0
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/bytedance/sonic v1.11.3 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.9.1 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.19.0 // indirect
Expand Down
13 changes: 12 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
github.com/bytedance/sonic v1.11.3 h1:jRN+yEjakWh8aK5FzrciUHG8OFXK+4/KrAX/ysEtHAA=
Expand All @@ -10,22 +12,27 @@ github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLI
github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0=
github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.19.0 h1:ol+5Fu+cSq9JD7SoSqe04GMI92cbn0+wvQ3bZ8b/AU4=
github.com/go-playground/validator/v10 v10.19.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-sql-driver/mysql v1.8.0 h1:UtktXaU2Nb64z/pLiGIxY4431SJ4/dR5cjMmlVHgnT4=
github.com/go-sql-driver/mysql v1.8.0/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
Expand All @@ -45,6 +52,7 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pelletier/go-toml/v2 v2.2.0 h1:QLgLl2yMN7N+ruc31VynXs1vhMZa7CeHHejIeBAsoHo=
github.com/pelletier/go-toml/v2 v2.2.0/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
Expand All @@ -56,6 +64,7 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
Expand All @@ -74,9 +83,11 @@ golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
Loading

0 comments on commit e566a1b

Please sign in to comment.