Passed
Push — main ( 26be31...19cade )
by Yume
01:25
created

routes.New   A

Complexity

Conditions 4

Size

Total Lines 60
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 36
nop 0
dl 0
loc 60
rs 9.016
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
package routes
2
3
import (
4
	"memnixrest/app/controllers"
5
6
	_ "memnixrest/docs"
7
8
	"github.com/gofiber/fiber/v2/middleware/cors"
9
10
	swagger "github.com/arsmn/fiber-swagger/v2"
11
	"github.com/gofiber/fiber/v2"
12
)
13
14
func New() *fiber.App {
15
	// Create new app
16
	app := fiber.New()
17
18
	app.Use(cors.New(cors.Config{
19
		AllowOrigins:     "http://localhost, *",
20
		AllowHeaders:     "Origin, Content-Type, Accept, Authorization",
21
		AllowCredentials: true,
22
	}))
23
24
	app.Get("/swagger/*", swagger.Handler) // default
25
26
	// Api group
27
	api := app.Group("/api")
28
29
	api.Get("/", func(c *fiber.Ctx) error {
30
		return fiber.NewError(fiber.StatusForbidden, "This is not a valid route") // Custom error
31
	})
32
33
	// Auth
34
	api.Post("/register", controllers.Register)
35
	api.Post("/login", controllers.Login)
36
	api.Get("/user", controllers.User)
37
	api.Post("/logout", controllers.Logout)
38
39
	// v1 group "/api/v1"
40
	v1 := api.Group("/v1", func(c *fiber.Ctx) error {
41
		return c.Next()
42
	})
43
44
	v1.Get("/", func(c *fiber.Ctx) error {
45
		return fiber.NewError(fiber.StatusForbidden, "This is not a valid route") // Custom error
46
	})
47
48
	// Register routes
49
	registerUserRoutes(v1) // /v1/users/
50
	registerDeckRoutes(v1) // /v1/decks/
51
	registerCardRoutes(v1) // /v1/cards/
52
53
	// Mem
54
	v1.Get("/mems/id/:id", controllers.GetMemByID)                             // Get mem by ID
55
	v1.Get("/mems/user/:userID/card/:cardID", controllers.GetMemByCardAndUser) // Get mem by userID & cardID
56
	v1.Post("/mems/new", controllers.CreateNewMem)                             // Create a new mem
57
	v1.Put("/mem/id/:id", controllers.UpdateMemByID)                           // Update mem by ID
58
59
	// Access
60
	v1.Get("/accesses", controllers.GetAllAccesses)                                       // Get all accesses
61
	v1.Get("/accesses/id/:id", controllers.GetAccessByID)                                 // Get access by ID
62
	v1.Get("/accesses/user/:userID/deck/:deckID", controllers.GetAccessByUserIDAndDeckID) // Get access by userID & deckID
63
	v1.Get("/accesses/user/:userID", controllers.GetAccessesByUserID)                     // Get accesses by userID
64
	v1.Post("/accesses/new", controllers.CreateNewAccess)                                 // Create a new access
65
	v1.Put("/accesses/id/:id", controllers.UpdateAccessByID)                              // Update an access using his ID
66
67
	// Answer
68
	v1.Get("/answers", controllers.GetAllAnswers)                   // Get all answers
69
	v1.Get("/answers/id/:id", controllers.GetAnswerByID)            // Get answer by ID
70
	v1.Get("/answers/card/:cardID", controllers.GetAnswersByCardID) // Get answer by CardID
71
	v1.Post("/answers/new", controllers.CreateNewAnswer)            // Create a new answer
72
73
	return app
74
}
75