2021-07-09 06:11:11 +00:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2021-08-03 18:14:32 +00:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2021-09-26 19:13:38 +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
|
2021-09-26 19:13:38 +00:00
|
|
|
d.App.Get("/", deps.SingleJoke)
|
2021-07-09 06:11:11 +00:00
|
|
|
|
|
|
|
// Today's joke
|
2021-09-26 19:13:38 +00:00
|
|
|
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
|
2021-09-26 19:13:38 +00:00
|
|
|
d.App.Get("/id/:id", middleware.OnlyIntegerAsID(), deps.JokeByID)
|
2021-07-17 14:37:01 +00:00
|
|
|
|
|
|
|
// Count total jokes
|
2021-09-26 19:13:38 +00:00
|
|
|
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
|
2021-09-26 19:13:38 +00:00
|
|
|
d.App.Put("/", middleware.RequireAuth(d.DB), deps.AddNewJoke)
|
2021-07-09 06:11:11 +00:00
|
|
|
|
|
|
|
// Update a joke
|
2021-09-26 19:13:38 +00:00
|
|
|
d.App.Patch("/id/:id", middleware.RequireAuth(d.DB), middleware.OnlyIntegerAsID(), deps.UpdateJoke)
|
2021-07-09 06:11:11 +00:00
|
|
|
|
|
|
|
// Delete a joke
|
2021-09-26 19:13:38 +00:00
|
|
|
d.App.Delete("/id/:id", middleware.RequireAuth(d.DB), middleware.OnlyIntegerAsID(), deps.DeleteJoke)
|
2021-07-09 06:11:11 +00:00
|
|
|
|
2021-09-26 19:13:38 +00:00
|
|
|
return d.App
|
2021-07-09 06:11:11 +00:00
|
|
|
}
|