Skip to content

Commit 22461de

Browse files
committed
# Refatorar e testes unitarios
1 parent 798a335 commit 22461de

File tree

9 files changed

+150
-30
lines changed

9 files changed

+150
-30
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Qodana
2+
on:
3+
workflow_dispatch:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
9+
jobs:
10+
qodana:
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: write
14+
pull-requests: write
15+
checks: write
16+
steps:
17+
- uses: actions/checkout@v3
18+
with:
19+
ref: ${{ github.event.pull_request.head.sha }}
20+
fetch-depth: 0
21+
- name: 'Qodana Scan'
22+
uses: JetBrains/[email protected]
23+
env:
24+
QODANA_TOKEN: ${{ secrets.QODANA_TOKEN }}

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/bullet-cloud-api.iml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/main.go

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
package main
22

33
import (
4-
"bullet-cloud-api/internal/handlers"
5-
"log"
6-
"net/http"
7-
"os"
4+
"bullet-cloud-api/internal/handlers"
5+
"log"
6+
"net/http"
7+
"os"
88

9-
"github.com/gorilla/mux"
10-
)
9+
"github.com/gorilla/mux"
10+
)
11+
12+
const defaultPort = "4444"
1113

1214
func main() {
13-
r := mux.NewRouter()
14-
r.HandleFunc("/health", handlers.HealthCheck).Methods("GET")
15-
r.HandleFunc("/products", handlers.GetAllProducts).Methods("GET")
16-
r.HandleFunc("/products", handlers.CreateProduct).Methods("POST")
17-
r.HandleFunc("/products/{id}", handlers.GetProduct).Methods("GET")
18-
r.HandleFunc("/products/{id}", handlers.UpdateProduct).Methods("PUT")
19-
r.HandleFunc("/products/{id}", handlers.DeleteProduct).Methods("DELETE")
20-
21-
port := os.Getenv("API_PORT")
22-
if port == "" {
23-
port = "4444" // Porta padrão, caso não esteja definida no ambiente
24-
}
25-
26-
log.Printf("Server starting on :%s", port)
27-
log.Fatal(http.ListenAndServe(":"+port, r))
15+
r := setupRoutes()
16+
17+
port := os.Getenv("API_PORT")
18+
if port == "" {
19+
port = defaultPort // Porta padrão definida para maior clareza
20+
}
21+
22+
log.Printf("Server starting on :%s", port)
23+
log.Fatal(http.ListenAndServe(":"+port, r))
24+
}
25+
26+
func setupRoutes() *mux.Router {
27+
r := mux.NewRouter()
28+
29+
r.HandleFunc("/health/{id}", handlers.HealthCheck).Methods("GET")
30+
r.HandleFunc("/products", handlers.GetAllProducts).Methods("GET")
31+
r.HandleFunc("/products", handlers.CreateProduct).Methods("POST")
32+
r.HandleFunc("/products/{id}", handlers.GetProduct).Methods("GET")
33+
r.HandleFunc("/products/{id}", handlers.UpdateProduct).Methods("PUT")
34+
r.HandleFunc("/products/{id}", handlers.DeleteProduct).Methods("DELETE")
35+
return r
2836
}

internal/handlers/product_handler.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,21 @@ var productRepo = models.NewProductRepository()
1313

1414
func HealthCheck(w http.ResponseWriter, r *http.Request) {
1515
w.Header().Set("Content-Type", "application/json")
16-
json.NewEncoder(w).Encode(map[string]string{"status": "healthy"})
16+
err := json.NewEncoder(w).Encode(map[string]string{"status": "healthy"})
17+
if err != nil {
18+
return
19+
}
1720
}
1821

1922
func GetAllProducts(w http.ResponseWriter, r *http.Request) {
2023
products := productRepo.GetAll()
2124
w.Header().Set("Content-Type", "application/json")
22-
json.NewEncoder(w).Encode(products)
25+
err := json.NewEncoder(w).Encode(products)
26+
if err != nil {
27+
return
28+
}
2329
}
24-
// bulletdev
30+
2531
func CreateProduct(w http.ResponseWriter, r *http.Request) {
2632
var product models.Product
2733

@@ -37,7 +43,9 @@ func CreateProduct(w http.ResponseWriter, r *http.Request) {
3743

3844
w.Header().Set("Content-Type", "application/json")
3945
w.WriteHeader(http.StatusCreated)
40-
json.NewEncoder(w).Encode(product)
46+
if err := json.NewEncoder(w).Encode(product); err != nil {
47+
http.Error(w, err.Error(), http.StatusInternalServerError)
48+
}
4149
}
4250

4351
func GetProduct(w http.ResponseWriter, r *http.Request) {
@@ -49,9 +57,12 @@ func GetProduct(w http.ResponseWriter, r *http.Request) {
4957
http.Error(w, err.Error(), http.StatusNotFound)
5058
return
5159
}
52-
// bulletdev
60+
5361
w.Header().Set("Content-Type", "application/json")
54-
json.NewEncoder(w).Encode(product)
62+
err = json.NewEncoder(w).Encode(product)
63+
if err != nil {
64+
return
65+
}
5566
}
5667

5768
func UpdateProduct(w http.ResponseWriter, r *http.Request) {
@@ -70,9 +81,11 @@ func UpdateProduct(w http.ResponseWriter, r *http.Request) {
7081
}
7182

7283
w.Header().Set("Content-Type", "application/json")
73-
json.NewEncoder(w).Encode(updatedProduct)
84+
if err := json.NewEncoder(w).Encode(updatedProduct); err != nil {
85+
http.Error(w, err.Error(), http.StatusInternalServerError)
86+
}
7487
}
75-
// bulletdev
88+
7689
func DeleteProduct(w http.ResponseWriter, r *http.Request) {
7790
vars := mux.Vars(r)
7891
id := vars["id"]

internal/handlers/product_handler_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package handlers
22

33
import (
44
"net/http"
5+
"net/http/httptest"
6+
"strings"
57
"testing"
68
)
79

@@ -14,7 +16,20 @@ func TestUpdateProduct(t *testing.T) {
1416
name string
1517
args args
1618
}{
17-
// TODO: Add test cases.
19+
{
20+
name: "Valid request",
21+
args: args{
22+
w: httptest.NewRecorder(),
23+
r: httptest.NewRequest("PUT", "/products/1", strings.NewReader(`{"name":"Updated Product"}`)),
24+
},
25+
},
26+
{
27+
name: "Invalid request",
28+
args: args{
29+
w: httptest.NewRecorder(),
30+
r: httptest.NewRequest("PUT", "/products/1", strings.NewReader(`{"invalid":"data"}`)),
31+
},
32+
},
1833
}
1934
for _, tt := range tests {
2035
t.Run(tt.name, func(t *testing.T) {

qodana.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#-------------------------------------------------------------------------------#
2+
# Qodana analysis is configured by qodana.yaml file #
3+
# https://www.jetbrains.com/help/qodana/qodana-yaml.html #
4+
#-------------------------------------------------------------------------------#
5+
version: "1.0"
6+
7+
#Specify inspection profile for code analysis
8+
profile:
9+
name: qodana.starter
10+
11+
#Enable inspections
12+
#include:
13+
# - name: <SomeEnabledInspectionId>
14+
15+
#Disable inspections
16+
#exclude:
17+
# - name: <SomeDisabledInspectionId>
18+
# paths:
19+
# - <path/where/not/run/inspection>
20+
21+
#Execute shell command before Qodana execution (Applied in CI/CD pipeline)
22+
#bootstrap: sh ./prepare-qodana.sh
23+
24+
#Install IDE plugins before Qodana execution (Applied in CI/CD pipeline)
25+
#plugins:
26+
# - id: <plugin.id> #(plugin id can be found at https://plugins.jetbrains.com)
27+
28+
#Specify Qodana linter for analysis (Applied in CI/CD pipeline)
29+
linter: jetbrains/qodana-go:2024.3

0 commit comments

Comments
 (0)