2023-10-28 17:50:44 -04:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2023-11-04 23:43:01 -04:00
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
|
2023-10-29 19:10:32 -04:00
|
|
|
"git.preston-baxter.com/Preston_PLB/capstone/frontend-service/db/models"
|
2023-10-28 17:50:44 -04:00
|
|
|
"git.preston-baxter.com/Preston_PLB/capstone/frontend-service/templates"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func LandingPage(c *gin.Context) {
|
2023-10-29 19:10:32 -04:00
|
|
|
if raw, exists := c.Get(USER_OBJ_KEY); exists {
|
|
|
|
if user, ok := raw.(*models.User); ok {
|
|
|
|
renderTempl(c, templates.LandingPage(user))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
renderTempl(c, templates.LandingPage(nil))
|
2023-10-28 17:50:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func LoginPage(c *gin.Context) {
|
|
|
|
renderTempl(c, templates.LoginPage(""))
|
|
|
|
}
|
|
|
|
|
|
|
|
func SignUpPage(c *gin.Context) {
|
|
|
|
renderTempl(c, templates.SignupPage(""))
|
|
|
|
}
|
|
|
|
|
|
|
|
func DashboardPage(c *gin.Context) {
|
2023-11-03 01:01:33 -04:00
|
|
|
user := getUserFromContext(c)
|
|
|
|
|
|
|
|
if user == nil {
|
|
|
|
log.Error("No user found in context")
|
2023-11-04 23:43:01 -04:00
|
|
|
serverError(c, "No user found in context")
|
2023-11-03 01:01:33 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-04 23:43:01 -04:00
|
|
|
//Split database fetching into go routines
|
|
|
|
var vendors []models.VendorAccount
|
|
|
|
var actions []models.ActionMapping
|
|
|
|
//TODO: find a generic way to do this.
|
|
|
|
errs := make([]error, 2)
|
|
|
|
|
|
|
|
//Use waitgroup to syncronize
|
|
|
|
waitGroup := new(sync.WaitGroup)
|
|
|
|
waitGroup.Add(2)
|
|
|
|
|
|
|
|
go func(wg *sync.WaitGroup){
|
2023-11-14 13:48:12 -05:00
|
|
|
vendors, errs[0] = mongo.FindAllVendorAccountsByUser(user.MongoId())
|
2023-11-04 23:43:01 -04:00
|
|
|
wg.Done()
|
|
|
|
}(waitGroup)
|
|
|
|
|
|
|
|
go func(wg *sync.WaitGroup){
|
|
|
|
actions, errs[1] = mongo.FindActionMappingsByUser(user.MongoId())
|
|
|
|
wg.Done()
|
|
|
|
}(waitGroup)
|
|
|
|
|
|
|
|
//after this line we are in sync
|
|
|
|
waitGroup.Wait()
|
|
|
|
|
|
|
|
//handle errors
|
|
|
|
for _, err := range errs {
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(errors.Join(errs...)).Error("Failed to do database lookup when retrieving dashbDashboardPage")
|
|
|
|
serverError(c, "Failed to do database lookup when retrieving dashbDashboardPage")
|
|
|
|
return
|
|
|
|
}
|
2023-10-29 21:20:59 -04:00
|
|
|
}
|
2023-11-03 01:01:33 -04:00
|
|
|
|
2023-11-04 23:43:01 -04:00
|
|
|
|
|
|
|
renderTempl(c, templates.DashboardPage(user, vendors, actions))
|
2023-10-28 17:50:44 -04:00
|
|
|
}
|