-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
491 lines (429 loc) · 14.4 KB
/
Copy pathmain.go
File metadata and controls
491 lines (429 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package main
import (
"os"
"net/http"
"github.com/gin-gonic/gin"
)
// projeto represents data about a record projeto.
type projeto struct {
ID_Projeto string `json:"id"`
Title string `json:"title"`
Description string `json:"Description"`
IDequipe []string `json:"equipe"`
}
type pessoa struct {
ID_Pessoa string `json:"id"`
Nome string `json:"nome"`
Profissao string `json:"profissao"`
ID_Equipe string `json:"equipe"`
ID_tarefa []string `json:"tarefa"`
}
type equipe struct {
Nome string `json:"nome"`
ID_Equipe string `json:"id"`
}
type tarefa struct {
ID_Tarefa string `json:"id"`
Nome string `json:"nome"`
Description string `json:"description"`
ID_Project string `json:"ID_Projeto"`
ID_Equipe string `json:"ID_Equipe"`
Tempo string `json:"tempo"`
}
// projetos slice to seed record projeto data.
var projetos = []projeto{
{ID_Projeto: "1", Title: "Central de Relacionamento", Description: "Sugestões", IDequipe: []string{"1", "3"}},
{ID_Projeto: "2", Title: "Jeru", Description: "talvez de certo", IDequipe: []string{"1", "3"}},
{ID_Projeto: "3", Title: "Sarah Vaughan and Clifford Brown", Description: "talvez de certo", IDequipe: []string{"1", "3"}},
}
var pessoas = []pessoa{
{ID_Pessoa: "1", Nome: "Bruno", Profissao: "Dev-Ops", ID_Equipe: "1", ID_tarefa: []string{"1", "4", "5"}},
{ID_Pessoa: "2", Nome: "Pedro", Profissao: "Back-End", ID_Equipe: "1", ID_tarefa: []string{"1", "3"}},
{ID_Pessoa: "3", Nome: "Caio", Profissao: "Front-End", ID_Equipe: "1", ID_tarefa: []string{"3", "2"}},
}
var equipes = []equipe{
{ID_Equipe: "1", Nome: "Komanda",},
{ID_Equipe: "2", Nome: "DevsCariri"},
{ID_Equipe: "3", Nome: "Kariri Inovação"},
}
var tarefas = []tarefa {
{ID_Tarefa: "1", Nome: "Criação de API REST", Description: "Utilizar GO LANG com Gin", ID_Project: "1", ID_Equipe: "", Tempo: ""},
{ID_Tarefa: "2", Nome: "Teste", Description: "Apenas Teste", ID_Project: "1", ID_Equipe: "", Tempo: ""},
{ID_Tarefa: "3", Nome: "Teste", Description: "Apenas Teste", ID_Project: "1", ID_Equipe: "", Tempo: ""},
{ID_Tarefa: "4", Nome: "Teste", Description: "Apenas Teste", ID_Project: "1", ID_Equipe: "", Tempo: ""},
{ID_Tarefa: "5", Nome: "Teste", Description: "Apenas Teste", ID_Project: "1", ID_Equipe: "", Tempo: ""},
}
var menu= []string{
"Bem vindo!"," ","Mais detalhes em: https://github.com/caiosousaf/api_desafio_BrisaNet",
"Aqui estão todas as rotas disponíveis:",
"-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=",
" ",
"-=-= PROJETOS =-=-",
" ",
"GET:","/projetos","/projetos/:id/tarefas/projetos/:id","/projetos/equipes/:id","/projetos/equipes/:id/members","/projetos/:id/equipes","POST:","/projetos","./projetos/:id/tarefa","PUT:","/projetos/:id","DELETE:","/projetos/:id",
" ", "------------------------------------"," ",
"-=-= EQUIPES =-=-",
" ",
"GET:","/equipes","/equipes/:id","/equipes/member/:id","POST:","/equipes","PUT:","/equipes/:id","DELETE:","/equipes/:id",
" ","------------------------------------"," ",
"-=-= MEMBROS =-=-",
" ",
"GET:","/pessoas","/pessoas/:id","/pessoas/:id/tarefas","POST:","/pessoas","PUT:","/pessoas/:id","DELETE:","/pessoas/:id",
" ", "------------------------------------"," ",
"-=-= TAREFAS =-=-",
" ",
"GET:","/tarefas","/tarefas/:id","/tarefas/:id/pessoas","POST:","/tarefas","PUT:","/tarefas/:id","DELETE:","/tarefas/:id",
" ",
"-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=",
}
func main() {
router := gin.Default()
router.GET("", gettelainicial)
router.GET("/projetos", getprojetos)
router.GET("/projetos/:id", getprojetoByID)
router.GET("/projetos/:id/tarefas", getTarefasByProject)
router.GET("/projetos/equipes/:id", getEquipeByID)
router.GET("/projetos/equipes/:id/members", getMembersInEquipeByID)
router.GET("/projetos/:id/equipes", getEquipeByProjetobyID)
router.POST("/projetos", postprojetos)
router.POST("/projetos/:id/tarefas", postTarefaProjeto)
router.PUT("/projetos/:id", editProjetoById)
router.DELETE("/projetos/:id", deleteProjetoById)
router.GET("/equipes", getEquipes)
router.GET("/equipes/:id", getEquipeByID)
router.GET("/equipes/member/:id", getMemberByID)
router.POST("/equipes", postEquipes)
router.PUT("/equipes/:id", updateEquipeById)
router.DELETE("/equipes/:id", deleteEquipeById)
router.GET("/pessoas", getPessoas)
router.GET("/pessoas/:id", getpessoaByID)
router.GET("/pessoas/:id/tarefas", getpessoaByIDthetaks)
router.POST("/pessoas", postpessoas)
router.PUT("/pessoas/:id", updatePessoaById)
router.DELETE("/pessoas/:id", deletePessoaById)
router.GET("/tarefas", getTarefas)
router.GET("/tarefas/:id", getTarefaByID)
router.GET("/tarefas/:id/pessoas", getTarefaBypeople)
router.POST("/tarefas", postTarefas)
router.PUT("/tarefas/:id", editTarefaById)
router.DELETE("/tarefas/:id", deleteTarefaById)
//router.Run("localhost:8090")
port := os.Getenv("PORT")
router.Run(":"+port)
}
// getprojetos/Pessoas/Equipes responds with the list of all projetos as JSON.
func gettelainicial(c *gin.Context){
c.IndentedJSON(http.StatusOK, menu)
}
func getprojetos(c *gin.Context) {
c.IndentedJSON(http.StatusOK, projetos)
}
func getPessoas(c *gin.Context) {
c.IndentedJSON(http.StatusOK, pessoas)
}
func getEquipes(c *gin.Context) {
c.IndentedJSON(http.StatusOK, equipes)
}
func getTarefas(c *gin.Context) {
c.IndentedJSON(http.StatusOK, tarefas)
}
// postprojetos adds an projeto from JSON received in the request body.
func postprojetos(c *gin.Context) {
var newprojeto projeto
// Call BindJSON to bind the received JSON to
// newprojeto.
if err := c.BindJSON(&newprojeto); err != nil {
return
}
// Add the new projeto to the slice.
projetos = append(projetos, newprojeto)
c.IndentedJSON(http.StatusCreated, newprojeto)
}
// getprojetoByID locates the projeto whose ID value matches the id
func getprojetoByID(c *gin.Context) {
id := c.Param("id")
for _, a := range projetos {
if a.ID_Projeto == id {
c.IndentedJSON(http.StatusOK, a)
return
}
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "projeto not found"})
}
func getEquipeByProjetobyID(c *gin.Context){
id := c.Param("id")
for _, a := range projetos {
if a.ID_Projeto == id {
c.IndentedJSON(http.StatusOK, a)
for _, d := range equipes{
for _, b:= range a.IDequipe{
if d.ID_Equipe == b{
c.IndentedJSON(http.StatusOK, d)
}
}
}
return
}
}
}
// Delete a project from the list of projects by ID
func deleteProjetoById(c *gin.Context) {
id := c.Param("id")
for i, a := range projetos {
if a.ID_Projeto == id {
projetos = append(projetos[:i], projetos[i+1:]... )
return
}
}
}
// Edit a project from the project list by ID
func editProjetoById(c *gin.Context) {
id := c.Param("id")
for i := range projetos {
if projetos[i].ID_Projeto == id {
c.BindJSON(&projetos[i])
c.IndentedJSON(http.StatusOK,projetos[i])
return
}
}
}
func postpessoas(c *gin.Context) {
var newpessoa pessoa
// Call BindJSON to bind the received JSON to
// newpessoa.
if err := c.BindJSON(&newpessoa); err != nil {
return
}
// Add the new pessoa to the slice.
pessoas = append(pessoas, newpessoa)
c.IndentedJSON(http.StatusCreated, newpessoa)
}
func postEquipes(c *gin.Context) {
var newequipe equipe
// Call BindJSON to bind the received JSON to newpessoa
if err := c.BindJSON(&newequipe); err != nil {
return
}
// Add the new pessoa to the slice.
equipes = append(equipes, newequipe)
c.IndentedJSON(http.StatusCreated, newequipe)
}
func deleteEquipeById(c *gin.Context) {
id := c.Param("id")
for i, a := range equipes {
if a.ID_Equipe == id {
equipes = append(equipes[:i], equipes[i+1:]... )
return
}
}
}
func updateEquipeById(c *gin.Context) {
id := c.Param("id")
for i := range equipes {
if equipes[i].ID_Equipe == id {
c.BindJSON(&equipes[i])
c.IndentedJSON(http.StatusOK,equipes[i])
return
}
}
}
func getpessoaByID(c *gin.Context) {
id := c.Param("id")
/* Loop through the list of pessoas, looking for
an pessoa whose ID value matches the parameter.*/
for _, a := range pessoas {
if a.ID_Pessoa == id {
c.IndentedJSON(http.StatusOK, a)
return
}
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "pessoa not found"})
}
func getMemberByID(c *gin.Context) {
id := c.Param("id")
/* Loop through the list of pessoas, looking for
an pessoa whose ID value matches the parameter.*/
for _, a := range pessoas {
if a.ID_Pessoa == id {
c.IndentedJSON(http.StatusOK, a)
return
}
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "Member not found"})
}
func getEquipeByID(c *gin.Context) {
id := c.Param("id")
for _, a := range equipes {
if a.ID_Equipe == id {
c.IndentedJSON(http.StatusOK, a)
return
}
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "Equipe not found"})
}
// shows the members of a team by id on the screen
func getMembersInEquipeByID(c *gin.Context) {
id := c.Param("id")
for _, a := range equipes {
if a.ID_Equipe == id {
for _, b := range pessoas {
if b.ID_Equipe == a.ID_Equipe {
c.IndentedJSON(http.StatusOK, b)
}
}
return
}
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "equipe not found"})
}
// Delete a person from the list of people by Id
func deletePessoaById(c *gin.Context) {
id := c.Param("id")
for i, a := range pessoas {
if a.ID_Pessoa == id {
pessoas = append(pessoas[:i], pessoas[i+1:]... )
return
}
}
}
// edit a person from a list of people via id
func updatePessoaById(c *gin.Context) {
id := c.Param("id")
for i := range pessoas {
if pessoas[i].ID_Pessoa == id {
c.BindJSON(&pessoas[i])
c.IndentedJSON(http.StatusOK,pessoas[i])
return
}
}
}
func getTarefaByID(c *gin.Context) {
id := c.Param("id")
for _, a := range tarefas {
if a.ID_Tarefa == id {
c.IndentedJSON(http.StatusOK, a)
return
}
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "Tarefa not found"})
}
func postTarefas(c *gin.Context) {
var newtarefa tarefa
// Call BindJSON to bind the received JSON to
// newpessoa.
if err := c.BindJSON(&newtarefa); err != nil {
return
}
// Add the new pessoa to the slice.
tarefas = append(tarefas, newtarefa)
c.IndentedJSON(http.StatusCreated, newtarefa)
}
func editTarefaById(c *gin.Context) {
id := c.Param("id")
for i := range tarefas {
if tarefas[i].ID_Tarefa == id {
c.BindJSON(&tarefas[i])
c.IndentedJSON(http.StatusOK,tarefas[i])
return
}
}
}
// // Delete a tarefa from the list of tarefas by Id
func deleteTarefaById(c *gin.Context) {
id := c.Param("id")
for i, a := range tarefas {
if a.ID_Tarefa == id {
tarefas = append(tarefas[:i], tarefas[i+1:]... )
return
}
}
}
func getTarefasByProject(c *gin.Context) {
id := c.Param("id")
count := 0
for _, a := range tarefas {
if a.ID_Project == id {
c.IndentedJSON(http.StatusOK, a)
count+=1
}
}
if(count > 0){
return
} else{
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "tarefa not found"})
}
}
// function publish a task in a given project
func postTarefaProjeto(c *gin.Context){
id := c.Param("id")
count := 0
for _, a := range projetos {
if a.ID_Projeto == id {
var newtarefa tarefa
// Call BindJSON to bind the received JSON to new tarefa
if err := c.BindJSON(&newtarefa); err != nil {
return
}
// Add the new Tarefa to the slice by Project.
tarefas = append(tarefas, newtarefa)
c.IndentedJSON(http.StatusCreated, newtarefa)
return
}
}
if(count > 0){
return
} else{
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "tarefa not found"})
}
}
//function that shows people who have a certain task
func getTarefaBypeople(c *gin.Context){
id := c.Param("id")
count := 0
for _, a := range pessoas {
outrocont := 0
for range a.ID_tarefa{
if a.ID_tarefa[outrocont] == id {
c.IndentedJSON(http.StatusOK, a)
count+=1
break
}
outrocont++
}
}
if(count > 0){
return
} else{
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "tarefa not found"})
}
}
//function that shows the tasks of a certain person
func getpessoaByIDthetaks(c *gin.Context){
id := c.Param("id")
count := 0
for _, a := range pessoas {
if a.ID_Pessoa == id {
c.IndentedJSON(http.StatusOK, a)
outrocont := 0
for range a.ID_tarefa{
for _, b := range tarefas {
if a.ID_tarefa[outrocont] == b.ID_Tarefa{
c.IndentedJSON(http.StatusOK, b)
outrocont+=1
}
}
}
if(outrocont > 0){
return
} else{
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "tarefa not found"})
}
count+=1
}
}
if(count > 0){
return
} else{
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "pessoa not found"})
}
}