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