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

Aula 2 #2

Open
wants to merge 2 commits into
base: Aula_1
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions assets/404.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@import url('https://fonts.googleapis.com/css?family=Alfa+Slab+One|Josefin+Slab');

body {
background-color: #d86c6c;
}

h1 {
font-family: 'Alfa Slab One', cursive;
margin: 0 auto;
margin-top: 10%;
text-align: center;
font-size: 70px;
}

h3 {
font-family: 'Josefin Slab', serif;
margin: 0 auto;
text-align: center;
font-size: 40px;
}

21 changes: 21 additions & 0 deletions assets/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@import url('https://fonts.googleapis.com/css?family=Alfa+Slab+One|Josefin+Slab');

body {
background-color: #aeb6bf;
}

h1 {
font-family: 'Alfa Slab One', cursive;
margin: 0 auto;
margin-top: 6%;
text-align: center;
font-size: 70px;
}

ul, li {
font-family: 'Josefin Slab', serif;
margin: 0 auto;
text-align: center;
font-size: 40px;
list-style: none;
}
16 changes: 14 additions & 2 deletions controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func CriarNovoAluno(c *gin.Context) {
"erro": err.Error()})
return
}
if err := models.ValidaDadosDeAluno(&aluno); err != nil{
if err := models.ValidaDadosDeAluno(&aluno); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"erro": err.Error()})
return
Expand Down Expand Up @@ -65,7 +65,7 @@ func EditarAluno(c *gin.Context) {
"erro": err.Error()})
return
}
if err := models.ValidaDadosDeAluno(&aluno); err != nil{
if err := models.ValidaDadosDeAluno(&aluno); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"erro": err.Error()})
return
Expand All @@ -86,3 +86,15 @@ func BuscaAlunoPorCPF(c *gin.Context) {
}
c.JSON(http.StatusOK, aluno)
}

func ExibePaginaIndex(c *gin.Context) {
var alunos []models.Aluno
database.DB.Find(&alunos)
c.HTML(http.StatusOK, "index.html", gin.H{
"alunos": alunos,
})
}

func RotaNaoEncontrada(c *gin.Context) {
c.HTML(http.StatusNotFound, "404.html", nil)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/ugorji/go v1.2.6 // indirect
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e // indirect
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMB
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
39 changes: 39 additions & 0 deletions go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
push:
branches: [ "Aula_1" ]
pull_request:
branches: [ "Aula_1" ]

jobs:

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: Build-DB
run: docker compose build

- name: Create-DB
run: docker compose up -d

- name: Test
run: go test -v main_teste.go

build:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build
run: go build -v main.go
121 changes: 121 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package main

import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"strconv"
"testing"

"github.com/gin-gonic/gin"
"github.com/guilhermeonrails/api-go-gin/controllers"
"github.com/guilhermeonrails/api-go-gin/database"
"github.com/guilhermeonrails/api-go-gin/models"
"github.com/stretchr/testify/assert"
)

var ID int

func SetupDasRotasDeTeste() *gin.Engine {
gin.SetMode(gin.ReleaseMode)
rotas := gin.Default()
return rotas
}

func CriaAlunoMock() {
aluno := models.Aluno{Nome: "Nome do Aluno Teste", CPF: "12345678901", RG: "123456789"}
database.DB.Create(&aluno)
ID = int(aluno.ID)
}

func DeletaAlunoMock() {
var aluno models.Aluno
database.DB.Delete(&aluno, ID)
}

func TestVerificaStatusCodeDaSaudacaoComParametro(t *testing.T) {
r := SetupDasRotasDeTeste()
r.GET("/:nome", controllers.Saudacoes)
req, _ := http.NewRequest("GET", "/gui", nil)
resposta := httptest.NewRecorder()
r.ServeHTTP(resposta, req)
assert.Equal(t, http.StatusOK, resposta.Code, "Deveriam ser iguais")
mockDaResposta := `{"API diz":"E ai gui, Tudo beleza?"}`
respostaBody, _ := ioutil.ReadAll(resposta.Body)
assert.Equal(t, mockDaResposta, string(respostaBody))
}

func TestListaTodosOsAlunosHanlder(t *testing.T) {
database.ConectaComBancoDeDados()
CriaAlunoMock()
defer DeletaAlunoMock()
r := SetupDasRotasDeTeste()
r.GET("/alunos", controllers.TodosAlunos)
req, _ := http.NewRequest("GET", "/alunos", nil)
resposta := httptest.NewRecorder()
r.ServeHTTP(resposta, req)
assert.Equal(t, http.StatusOK, resposta.Code)
}

func TestBucaAlunoPorCPFHandler(t *testing.T) {
database.ConectaComBancoDeDados()
CriaAlunoMock()
defer DeletaAlunoMock()
r := SetupDasRotasDeTeste()
r.GET("/alunos/cpf/:cpf", controllers.BuscaAlunoPorCPF)
req, _ := http.NewRequest("GET", "/alunos/cpf/12345678901", nil)
resposta := httptest.NewRecorder()
r.ServeHTTP(resposta, req)
assert.Equal(t, http.StatusOK, resposta.Code)
}

func TestBuscaAlunoPorIDHandler(t *testing.T) {
database.ConectaComBancoDeDados()
CriaAlunoMock()
defer DeletaAlunoMock()
r := SetupDasRotasDeTeste()
r.GET("/alunos/:id", controllers.BuscarAlunoPorID)
pathDaBusca := "/alunos/" + strconv.Itoa(ID)
req, _ := http.NewRequest("GET", pathDaBusca, nil)
resposta := httptest.NewRecorder()
r.ServeHTTP(resposta, req)
var alunoMock models.Aluno
json.Unmarshal(resposta.Body.Bytes(), &alunoMock)
assert.Equal(t, "Nome do Aluno Teste", alunoMock.Nome, "Os nomes devem ser iguais")
assert.Equal(t, "12345678901", alunoMock.CPF)
assert.Equal(t, "123456789", alunoMock.RG)
assert.Equal(t, http.StatusOK, resposta.Code)
}

func TestDeletaAlunoHandler(t *testing.T) {
database.ConectaComBancoDeDados()
CriaAlunoMock()
r := SetupDasRotasDeTeste()
r.DELETE("/alunos/:id", controllers.DeletarAluno)
pathDeBusca := "/alunos/" + strconv.Itoa(ID)
req, _ := http.NewRequest("DELETE", pathDeBusca, nil)
resposta := httptest.NewRecorder()
r.ServeHTTP(resposta, req)
assert.Equal(t, http.StatusOK, resposta.Code)
}

func TestEditaUmAlunoHandler(t *testing.T) {
database.ConectaComBancoDeDados()
CriaAlunoMock()
defer DeletaAlunoMock()
r := SetupDasRotasDeTeste()
r.PATCH("/alunos/:id", controllers.EditarAluno)
aluno := models.Aluno{Nome: "Nome do Aluno Teste", CPF: "47123456789", RG: "123456700"}
valorJson, _ := json.Marshal(aluno)
pathParaEditar := "/alunos/" + strconv.Itoa(ID)
req, _ := http.NewRequest("PATCH", pathParaEditar, bytes.NewBuffer(valorJson))
resposta := httptest.NewRecorder()
r.ServeHTTP(resposta, req)
var alunoMockAtualizado models.Aluno
json.Unmarshal(resposta.Body.Bytes(), &alunoMockAtualizado)
assert.Equal(t, "47123456789", alunoMockAtualizado.CPF)
assert.Equal(t, "123456700", alunoMockAtualizado.RG)
assert.Equal(t, "Nome do Aluno Teste", alunoMockAtualizado.Nome)
}
Binary file modified postgres-data/base/16384/1259
Binary file not shown.
Binary file modified postgres-data/base/16384/16385
Binary file not shown.
Binary file modified postgres-data/base/16384/16386
Binary file not shown.
Binary file added postgres-data/base/16384/16386_fsm
Binary file not shown.
Binary file added postgres-data/base/16384/16386_vm
Binary file not shown.
Binary file modified postgres-data/base/16384/16392
Binary file not shown.
Binary file modified postgres-data/base/16384/16394
Binary file not shown.
Binary file modified postgres-data/base/16384/2619
Binary file not shown.
Binary file modified postgres-data/base/16384/2619_fsm
Binary file not shown.
Binary file modified postgres-data/base/16384/2619_vm
Binary file not shown.
Binary file modified postgres-data/base/16384/2696
Binary file not shown.
Binary file modified postgres-data/global/pg_control
Binary file not shown.
Binary file added postgres-data/pg_stat/db_0.stat
Binary file not shown.
Binary file added postgres-data/pg_stat/db_16384.stat
Binary file not shown.
Binary file added postgres-data/pg_stat/global.tmp
Binary file not shown.
Binary file removed postgres-data/pg_stat_tmp/db_0.stat
Binary file not shown.
Binary file removed postgres-data/pg_stat_tmp/db_16384.stat
Binary file not shown.
Binary file modified postgres-data/pg_stat_tmp/global.stat
Binary file not shown.
Binary file modified postgres-data/pg_wal/000000010000000000000001
Binary file not shown.
Binary file modified postgres-data/pg_xact/0000
Binary file not shown.
8 changes: 8 additions & 0 deletions postgres-data/postmaster.pid
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
1
/var/lib/postgresql/data
1642346010
5432
/var/run/postgresql
*
58478122 0
stopping
4 changes: 4 additions & 0 deletions routes/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

func HandleRequest() {
r := gin.Default()
r.LoadHTMLGlob("templates/*")
r.Static("/assets", "./assets")
r.GET("/:nome", controllers.Saudacoes)
r.GET("/alunos", controllers.TodosAlunos)
r.GET("/alunos/:id", controllers.BuscarAlunoPorID)
Expand All @@ -15,5 +17,7 @@ func HandleRequest() {
r.PATCH("/alunos/:id", controllers.EditarAluno)
r.GET("/alunos/cpf/:cpf", controllers.BuscaAlunoPorCPF)
r.GET("/alunos/", controllers.BuscaAlunoPorCPF)
r.GET("/index", controllers.ExibePaginaIndex)
r.NoRoute(controllers.RotaNaoEncontrada)
r.Run()
}
14 changes: 14 additions & 0 deletions templates/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" href="../assets/404.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>404</title>
</head>
<body>
<h1>404</h1>
<h3>Procuramos em toda aplicação e não encontramos nada...</h3>
</body>
</html>
21 changes: 21 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" href="../assets/index.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Alunos</title>
</head>
<body>
<div class="widget-wrap">
<h1>API - Gin e Golang</h1>
<div class="widget-wrap">
<ul id="the-list">
{{range .alunos}}
<li>{{.Nome}}</li>
{{end}}
</ul>
</div>
</body>
</html>