Skip to content

Commit

Permalink
clean up server params
Browse files Browse the repository at this point in the history
  • Loading branch information
DOOduneye committed Mar 4, 2024
1 parent e1d6c2d commit d625ed0
Show file tree
Hide file tree
Showing 39 changed files with 537 additions and 337 deletions.
44 changes: 44 additions & 0 deletions backend/src/email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
type EmailServiceInterface interface {
SendPasswordResetEmail(name, email, token string) *errors.Error
SendEmailVerification(email, code string) *errors.Error
SendWelcomeEmail(name, email string) *errors.Error
SendPasswordChangedEmail(name, email string) *errors.Error
}

type EmailService struct {
Expand Down Expand Up @@ -66,6 +68,48 @@ func (e *EmailService) SendEmailVerification(email, code string) *errors.Error {
return nil
}

func (e *EmailService) SendWelcomeEmail(name, email string) *errors.Error {
template, err := getTemplateString("welcome")
if err != nil {
return &errors.FailedToGetTemplate
}

params := &resend.SendEmailRequest{
From: "[email protected]",
To: []string{email},
Subject: "Welcome to Resend",
Html: fmt.Sprintf(*template, name),
}

_, err = e.Client.Emails.Send(params)
if err != nil {
return &errors.FailedToSendEmail
}

return nil
}

func (e *EmailService) SendPasswordChangedEmail(name, email string) *errors.Error {
template, err := getTemplateString("password_change_complete")
if err != nil {
return &errors.FailedToGetTemplate
}

params := &resend.SendEmailRequest{
From: "[email protected]",
To: []string{email},
Subject: "Password Changed",
Html: fmt.Sprintf(*template, name),
}

_, err = e.Client.Emails.Send(params)
if err != nil {
return &errors.FailedToSendEmail
}

return nil
}

func getTemplateString(name string) (*string, error) {
// TODO: use default file location
cwd, err := os.Getwd()
Expand Down
12 changes: 5 additions & 7 deletions backend/src/server/routes/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@ package routes
import (
"time"

"github.com/GenerateNU/sac/backend/src/config"
"github.com/GenerateNU/sac/backend/src/controllers"
"github.com/GenerateNU/sac/backend/src/middleware"
"github.com/GenerateNU/sac/backend/src/services"
"github.com/gofiber/fiber/v2"
"github.com/GenerateNU/sac/backend/src/types"
)

func Auth(router fiber.Router, authService services.AuthServiceInterface, settings config.AuthSettings, authMiddleware *middleware.AuthMiddlewareService) {
authController := controllers.NewAuthController(authService, settings)
func Auth(params types.RouteParams) {
authController := controllers.NewAuthController(services.NewAuthService(params.ServiceParams), params.Settings)

// api/v1/auth/*
auth := router.Group("/auth")
auth := params.Router.Group("/auth")

auth.Post("/login", authController.Login)
auth.Get("/logout", authController.Logout)
auth.Get("/refresh", authController.Refresh)
auth.Get("/me", authController.Me)
auth.Post("/update-password/:userID", authMiddleware.Limiter(2, 1*time.Minute), authMiddleware.UserAuthorizeById, authController.UpdatePassword)
auth.Post("/update-password/:userID", params.AuthMiddleware.Limiter(2, 1*time.Minute), params.AuthMiddleware.UserAuthorizeById, authController.UpdatePassword)
auth.Post("/send-code/:userID", authController.SendCode)
auth.Post("/verify-email", authController.VerifyEmail)
auth.Post("/forgot-password", authController.ForgotPassword)
Expand Down
25 changes: 13 additions & 12 deletions backend/src/server/routes/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,35 @@ package routes
import (
"github.com/GenerateNU/sac/backend/src/auth"
"github.com/GenerateNU/sac/backend/src/controllers"
"github.com/GenerateNU/sac/backend/src/middleware"
"github.com/GenerateNU/sac/backend/src/services"
"github.com/go-playground/validator/v10"
"github.com/GenerateNU/sac/backend/src/types"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
)

func CategoryRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate, authMiddleware *middleware.AuthMiddlewareService) {
categoryIDRoute := Category(router, services.NewCategoryService(db, validate), authMiddleware)
func CategoryRoutes(categoryParams types.RouteParams) {
categoryIDRoute := Category(categoryParams)

CategoryTag(categoryIDRoute, services.NewCategoryTagService(db, validate))
// update the router in params
categoryParams.Router = categoryIDRoute

CategoryTag(categoryParams)
}

func Category(router fiber.Router, categoryService services.CategoryServiceInterface, authMiddleware *middleware.AuthMiddlewareService) fiber.Router {
categoryController := controllers.NewCategoryController(categoryService)
func Category(categoryParams types.RouteParams) fiber.Router {
categoryController := controllers.NewCategoryController(services.NewCategoryService(categoryParams.ServiceParams))

// api/v1/categories/*
categories := router.Group("/categories")
categories := categoryParams.Router.Group("/categories")

categories.Post("/", authMiddleware.Authorize(auth.CreateAll), categoryController.CreateCategory)
categories.Post("/", categoryParams.AuthMiddleware.Authorize(auth.CreateAll), categoryController.CreateCategory)
categories.Get("/", categoryController.GetCategories)

// api/v1/categories/:categoryID/*
categoriesID := categories.Group("/:categoryID")

categoriesID.Get("/", categoryController.GetCategory)
categoriesID.Delete("/", authMiddleware.Authorize(auth.DeleteAll), categoryController.DeleteCategory)
categoriesID.Patch("/", authMiddleware.Authorize(auth.WriteAll), categoryController.UpdateCategory)
categoriesID.Delete("/", categoryParams.AuthMiddleware.Authorize(auth.DeleteAll), categoryController.DeleteCategory)
categoriesID.Patch("/", categoryParams.AuthMiddleware.Authorize(auth.WriteAll), categoryController.UpdateCategory)

return categoriesID
}
8 changes: 4 additions & 4 deletions backend/src/server/routes/category_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package routes
import (
"github.com/GenerateNU/sac/backend/src/controllers"
"github.com/GenerateNU/sac/backend/src/services"
"github.com/gofiber/fiber/v2"
"github.com/GenerateNU/sac/backend/src/types"
)

func CategoryTag(categoryIDRoute fiber.Router, categoryTagService services.CategoryTagServiceInterface) {
categoryTagController := controllers.NewCategoryTagController(categoryTagService)
func CategoryTag(categoryParams types.RouteParams) {
categoryTagController := controllers.NewCategoryTagController(services.NewCategoryTagService(categoryParams.ServiceParams))

// api/v1/categories/:categoryID/tags/*
categoryTags := categoryIDRoute.Group("/tags")
categoryTags := categoryParams.Router.Group("/tags")

categoryTags.Get("/", categoryTagController.GetTagsByCategory)
categoryTags.Get("/:tagID", categoryTagController.GetTagByCategory)
Expand Down
33 changes: 17 additions & 16 deletions backend/src/server/routes/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,39 @@ package routes
import (
p "github.com/GenerateNU/sac/backend/src/auth"
"github.com/GenerateNU/sac/backend/src/controllers"
"github.com/GenerateNU/sac/backend/src/middleware"
"github.com/GenerateNU/sac/backend/src/services"
"github.com/go-playground/validator/v10"
"github.com/GenerateNU/sac/backend/src/types"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
)

func ClubRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate, authMiddleware *middleware.AuthMiddlewareService) {
clubIDRouter := Club(router, services.NewClubService(db, validate), authMiddleware)
func ClubRoutes(clubParams types.RouteParams) {
clubIDRouter := Club(clubParams)

ClubTag(clubIDRouter, services.NewClubTagService(db, validate), authMiddleware)
ClubFollower(clubIDRouter, services.NewClubFollowerService(db), authMiddleware)
ClubMember(clubIDRouter, services.NewClubMemberService(db, validate), authMiddleware)
ClubContact(clubIDRouter, services.NewClubContactService(db, validate), authMiddleware)
ClubEvent(clubIDRouter, services.NewClubEventService(db))
// update the router in params
clubParams.Router = clubIDRouter

ClubTag(clubParams)
ClubFollower(clubParams)
ClubMember(clubParams)
ClubContact(clubParams)
ClubEvent(clubParams)
}

func Club(router fiber.Router, clubService services.ClubServiceInterface, authMiddleware *middleware.AuthMiddlewareService) fiber.Router {
clubController := controllers.NewClubController(clubService)
func Club(clubParams types.RouteParams) fiber.Router {
clubController := controllers.NewClubController(services.NewClubService(clubParams.ServiceParams))

// api/v1/clubs/*
clubs := router.Group("/clubs")
clubs := clubParams.Router.Group("/clubs")

clubs.Get("/", clubController.GetClubs)
clubs.Post("/", authMiddleware.Authorize(p.CreateAll), clubController.CreateClub)
clubs.Post("/", clubParams.AuthMiddleware.Authorize(p.CreateAll), clubController.CreateClub)

// api/v1/clubs/:clubID/*
clubsID := clubs.Group("/:clubID")

clubsID.Get("/", clubController.GetClub)
clubsID.Patch("/", authMiddleware.ClubAuthorizeById, clubController.UpdateClub)
clubsID.Delete("/", authMiddleware.Authorize(p.DeleteAll), clubController.DeleteClub)
clubsID.Patch("/", clubParams.AuthMiddleware.ClubAuthorizeById, clubController.UpdateClub)
clubsID.Delete("/", clubParams.AuthMiddleware.Authorize(p.DeleteAll), clubController.DeleteClub)

return clubsID
}
11 changes: 5 additions & 6 deletions backend/src/server/routes/club_contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package routes

import (
"github.com/GenerateNU/sac/backend/src/controllers"
"github.com/GenerateNU/sac/backend/src/middleware"
"github.com/GenerateNU/sac/backend/src/services"
"github.com/gofiber/fiber/v2"
"github.com/GenerateNU/sac/backend/src/types"
)

func ClubContact(clubsIDRouter fiber.Router, clubContactService services.ClubContactServiceInterface, authMiddleware *middleware.AuthMiddlewareService) {
clubContactController := controllers.NewClubContactController(clubContactService)
func ClubContact(clubParams types.RouteParams) {
clubContactController := controllers.NewClubContactController(services.NewClubContactService(clubParams.ServiceParams))

clubContacts := clubsIDRouter.Group("/contacts")
clubContacts := clubParams.Router.Group("/contacts")

// api/v1/clubs/:clubID/contacts/*
clubContacts.Get("/", clubContactController.GetClubContacts)
clubContacts.Put("/", authMiddleware.ClubAuthorizeById, clubContactController.PutContact)
clubContacts.Put("/", clubParams.AuthMiddleware.ClubAuthorizeById, clubContactController.PutContact)
}
8 changes: 4 additions & 4 deletions backend/src/server/routes/club_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package routes
import (
"github.com/GenerateNU/sac/backend/src/controllers"
"github.com/GenerateNU/sac/backend/src/services"
"github.com/gofiber/fiber/v2"
"github.com/GenerateNU/sac/backend/src/types"
)

func ClubEvent(clubIDRouter fiber.Router, clubEventService services.ClubEventServiceInterface) {
clubEventController := controllers.NewClubEventController(clubEventService)
func ClubEvent(clubParams types.RouteParams) {
clubEventController := controllers.NewClubEventController(services.NewClubEventService(clubParams.ServiceParams))

// api/v1/clubs/:clubID/events/*
events := clubIDRouter.Group("/events")
events := clubParams.Router.Group("/events")

events.Get("/", clubEventController.GetClubEvents)
}
11 changes: 5 additions & 6 deletions backend/src/server/routes/club_follower.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package routes

import (
"github.com/GenerateNU/sac/backend/src/controllers"
"github.com/GenerateNU/sac/backend/src/middleware"
"github.com/GenerateNU/sac/backend/src/services"
"github.com/gofiber/fiber/v2"
"github.com/GenerateNU/sac/backend/src/types"
)

func ClubFollower(clubsIDRouter fiber.Router, clubFollowerService services.ClubFollowerServiceInterface, authMiddleware *middleware.AuthMiddlewareService) {
clubFollowerController := controllers.NewClubFollowerController(clubFollowerService)
func ClubFollower(clubParams types.RouteParams) {
clubFollowerController := controllers.NewClubFollowerController(services.NewClubFollowerService(clubParams.ServiceParams))

clubFollower := clubsIDRouter.Group("/followers")
clubFollower := clubParams.Router.Group("/followers")

// api/clubs/:clubID/followers/*
clubFollower.Get("/", authMiddleware.ClubAuthorizeById, clubFollowerController.GetClubFollowers)
clubFollower.Get("/", clubParams.AuthMiddleware.ClubAuthorizeById, clubFollowerController.GetClubFollowers)
}
11 changes: 5 additions & 6 deletions backend/src/server/routes/club_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package routes

import (
"github.com/GenerateNU/sac/backend/src/controllers"
"github.com/GenerateNU/sac/backend/src/middleware"
"github.com/GenerateNU/sac/backend/src/services"
"github.com/gofiber/fiber/v2"
"github.com/GenerateNU/sac/backend/src/types"
)

func ClubMember(clubsIDRouter fiber.Router, clubMemberService services.ClubMemberServiceInterface, authMiddleware *middleware.AuthMiddlewareService) {
clubMemberController := controllers.NewClubMemberController(clubMemberService)
func ClubMember(clubParams types.RouteParams) {
clubMemberController := controllers.NewClubMemberController(services.NewClubMemberService(clubParams.ServiceParams))

clubMember := clubsIDRouter.Group("/members")
clubMember := clubParams.Router.Group("/members")

// api/v1/clubs/:clubID/members/*
clubMember.Get("/", authMiddleware.ClubAuthorizeById, clubMemberController.GetClubMembers)
clubMember.Get("/", clubParams.AuthMiddleware.ClubAuthorizeById, clubMemberController.GetClubMembers)
}
13 changes: 6 additions & 7 deletions backend/src/server/routes/club_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package routes

import (
"github.com/GenerateNU/sac/backend/src/controllers"
"github.com/GenerateNU/sac/backend/src/middleware"
"github.com/GenerateNU/sac/backend/src/services"
"github.com/gofiber/fiber/v2"
"github.com/GenerateNU/sac/backend/src/types"
)

func ClubTag(clubIDRouter fiber.Router, clubTagService services.ClubTagServiceInterface, authMiddleware *middleware.AuthMiddlewareService) {
clubTagController := controllers.NewClubTagController(clubTagService)
func ClubTag(clubParams types.RouteParams) {
clubTagController := controllers.NewClubTagController(services.NewClubTagService(clubParams.ServiceParams))

clubTags := clubIDRouter.Group("/tags")
clubTags := clubParams.Router.Group("/tags")

clubTags.Get("/", clubTagController.GetClubTags)
clubTags.Post("/", authMiddleware.ClubAuthorizeById, clubTagController.CreateClubTags)
clubTags.Delete("/:tagID", authMiddleware.ClubAuthorizeById, clubTagController.DeleteClubTag)
clubTags.Post("/", clubParams.AuthMiddleware.ClubAuthorizeById, clubTagController.CreateClubTags)
clubTags.Delete("/:tagID", clubParams.AuthMiddleware.ClubAuthorizeById, clubTagController.DeleteClubTag)
}
11 changes: 5 additions & 6 deletions backend/src/server/routes/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package routes

import (
"github.com/GenerateNU/sac/backend/src/controllers"
"github.com/GenerateNU/sac/backend/src/middleware"
"github.com/GenerateNU/sac/backend/src/services"
"github.com/gofiber/fiber/v2"
"github.com/GenerateNU/sac/backend/src/types"
)

func Contact(router fiber.Router, contactService services.ContactServiceInterface, authMiddleware *middleware.AuthMiddlewareService) {
contactController := controllers.NewContactController(contactService)
func Contact(contactParams types.RouteParams) {
contactController := controllers.NewContactController(services.NewContactService(contactParams.ServiceParams))

// api/v1/contacts/*
contacts := router.Group("/contacts")
contacts := contactParams.Router.Group("/contacts")

contacts.Get("/", contactController.GetContacts)
contacts.Get("/:contactID", contactController.GetContact)
contacts.Delete("/:contactID", authMiddleware.UserAuthorizeById, contactController.DeleteContact)
contacts.Delete("/:contactID", contactParams.AuthMiddleware.UserAuthorizeById, contactController.DeleteContact)
}
21 changes: 10 additions & 11 deletions backend/src/server/routes/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@ package routes

import (
"github.com/GenerateNU/sac/backend/src/controllers"
"github.com/GenerateNU/sac/backend/src/middleware"
"github.com/GenerateNU/sac/backend/src/services"
"github.com/gofiber/fiber/v2"
"github.com/GenerateNU/sac/backend/src/types"
)

func Event(router fiber.Router, eventService services.EventServiceInterface, authMiddleware *middleware.AuthMiddlewareService) {
eventController := controllers.NewEventController(eventService)
func Event(eventParams types.RouteParams) {
eventController := controllers.NewEventController(services.NewEventService(eventParams.ServiceParams))

// api/v1/events/*
events := router.Group("/events")
events := eventParams.Router.Group("/events")

events.Get("/", eventController.GetAllEvents)
events.Post("/", authMiddleware.ClubAuthorizeById, eventController.CreateEvent)
events.Post("/", eventParams.AuthMiddleware.ClubAuthorizeById, eventController.CreateEvent)

// api/v1/events/:eventID/*
eventID := events.Group("/:eventID")

eventID.Get("/", eventController.GetEvent)
eventID.Get("/series", eventController.GetSeriesByEventID)
events.Patch("/", authMiddleware.ClubAuthorizeById, eventController.UpdateEvent)
eventID.Delete("/", authMiddleware.ClubAuthorizeById, eventController.DeleteEvent)
eventID.Delete("/series", authMiddleware.ClubAuthorizeById, eventController.DeleteSeriesByEventID)
events.Patch("/", eventParams.AuthMiddleware.ClubAuthorizeById, eventController.UpdateEvent)
eventID.Delete("/", eventParams.AuthMiddleware.ClubAuthorizeById, eventController.DeleteEvent)
eventID.Delete("/series", eventParams.AuthMiddleware.ClubAuthorizeById, eventController.DeleteSeriesByEventID)

// api/v1/events/:eventID/series/*
series := events.Group("/series")
Expand All @@ -38,6 +37,6 @@ func Event(router fiber.Router, eventService services.EventServiceInterface, aut
seriesID := series.Group("/:seriesID")

seriesID.Get("/", eventController.GetSeriesByID)
seriesID.Patch("/", authMiddleware.ClubAuthorizeById, eventController.UpdateSeriesByID)
seriesID.Delete("/", authMiddleware.ClubAuthorizeById, eventController.DeleteSeriesByID)
seriesID.Patch("/", eventParams.AuthMiddleware.ClubAuthorizeById, eventController.UpdateSeriesByID)
seriesID.Delete("/", eventParams.AuthMiddleware.ClubAuthorizeById, eventController.DeleteSeriesByID)
}
Loading

0 comments on commit d625ed0

Please sign in to comment.