jokes-bapak2/api/app/v1/routes/joke.go

43 lines
1.0 KiB
Go
Raw Normal View History

2021-07-09 06:11:11 +00:00
package routes
import (
"jokes-bapak2-api/app/v1/handler/joke"
2021-07-14 18:17:01 +00:00
"jokes-bapak2-api/app/v1/middleware"
2021-08-04 09:03:53 +00:00
"time"
2021-07-09 06:11:11 +00:00
"github.com/gofiber/fiber/v2"
2021-08-04 09:03:53 +00:00
"github.com/gofiber/fiber/v2/middleware/cache"
2021-07-09 06:11:11 +00:00
)
func (d *Dependencies) Joke() *fiber.App {
deps := joke.Dependencies{
DB: d.DB,
Redis: d.Redis,
Memory: d.Memory,
HTTP: d.HTTP,
Query: d.Query,
}
2021-07-09 06:11:11 +00:00
// Single route
d.App.Get("/", deps.SingleJoke)
2021-07-09 06:11:11 +00:00
// Today's joke
d.App.Get("/today", cache.New(cache.Config{Expiration: 6 * time.Hour}), deps.TodayJoke)
2021-07-09 06:11:11 +00:00
// Joke by ID
d.App.Get("/id/:id", middleware.OnlyIntegerAsID(), deps.JokeByID)
2021-07-17 14:37:01 +00:00
// Count total jokes
d.App.Get("/total", cache.New(cache.Config{Expiration: 15 * time.Minute}), deps.TotalJokes)
2021-07-09 06:11:11 +00:00
// Add new joke
d.App.Put("/", middleware.RequireAuth(d.DB), deps.AddNewJoke)
2021-07-09 06:11:11 +00:00
// Update a joke
d.App.Patch("/id/:id", middleware.RequireAuth(d.DB), middleware.OnlyIntegerAsID(), deps.UpdateJoke)
2021-07-09 06:11:11 +00:00
// Delete a joke
d.App.Delete("/id/:id", middleware.RequireAuth(d.DB), middleware.OnlyIntegerAsID(), deps.DeleteJoke)
2021-07-09 06:11:11 +00:00
return d.App
2021-07-09 06:11:11 +00:00
}