Potion/internal/app/service/engagement_service.go
Hayden Hargreaves d29426290d (FEAT): Created engagement buttons, and wired most of them up!
The make button is pretty much done, just need to finish up by
rate limiting it. That will take place in the engagement repository.

Sharing works as well, just a UI change, there is no backend yet, maybe
there should be an engagement type for sharing recipes. But not totally
sure.

The viewing is also in a semi-working state. It does not create requests
for users that aren't signed in, which needs to come. With that update
there is a need to update HOW the requests come in, we don't need it
every time we load the page. Maybe just when we click to it, from
somewhere else?

Finally, the favoriting does not totally work. The entry into the
engagement table is complete, but the actual favorites table, favorite
creation, button toggling AND button rendering is not implemented yet.
2025-07-14 21:00:28 -07:00

75 lines
3.0 KiB
Go

package service
import (
"fmt"
domain "github.com/haydenhargreaves/Potion/internal/domain/engagement"
domainRecipe "github.com/haydenhargreaves/Potion/internal/domain/recipe"
_ "github.com/lib/pq"
)
type EngagementService struct {
engagementRepository domain.EngagementRepository
recipeRepository domainRecipe.RecipeRepository
}
// Compile-time check to ensure the EngagementService implements domain.EngagementService
var _ domain.EngagementService = (*EngagementService)(nil)
// NewUserRepository creates a user repository object which is used by the user service to access
// the database. Any user related database operations will take place in this repository.
func NewEngagementService(engagementRepository domain.EngagementRepository, recipeRepository domainRecipe.RecipeRepository) domain.EngagementService {
return &EngagementService{
engagementRepository: engagementRepository,
recipeRepository: recipeRepository,
}
}
// UserViewRecipe requires a user ID and a recipe ID to create an engagement record in the database.
// A message will be generated using the recipe data and then used to add a view engagement to the
// database.
func (s *EngagementService) UserViewRecipe(userId, recipeId int) (domain.Engagement, error) {
recipe, err := s.recipeRepository.GetRecipe(recipeId)
if err != nil {
return domain.Engagement{}, err
}
message := fmt.Sprintf("Viewed \"%s\"", recipe.Title)
return s.engagementRepository.AddUserEntityEngagement(userId, recipeId, message, domain.EngagementViewed)
}
// UserLikeRecipe requires a user ID and a recipe ID to create an engagement record in the database.
// A message will be generated using the recipe data and then used to add a like engagement to the
// database.
func (s *EngagementService) UserLikeRecipe(userId, recipeId int) (domain.Engagement, error) {
recipe, err := s.recipeRepository.GetRecipe(recipeId)
if err != nil {
return domain.Engagement{}, err
}
message := fmt.Sprintf("Liked \"%s\"", recipe.Title)
return s.engagementRepository.AddUserEntityEngagement(userId, recipeId, message, domain.EngagementLiked)
}
// UserLikeRecipe requires a user ID and a recipe ID to create an engagement record in the database.
// A message will be generated using the recipe data and then used to add a like engagement to the
// database.
func (s *EngagementService) UserMakeRecipe(userId, recipeId int) (domain.Engagement, error) {
recipe, err := s.recipeRepository.GetRecipe(recipeId)
if err != nil {
return domain.Engagement{}, err
}
message := fmt.Sprintf("Made \"%s\"", recipe.Title)
return s.engagementRepository.AddUserEntityEngagement(userId, recipeId, message, domain.EngagementMade)
}
// GetUserEngagement returns a list of the users most recent engagement entries. The number of records
// is determined by the limit passed into this function. The results are sorted, newest-to-oldest.
func (s *EngagementService) GetUserEngagement(userId, limit int) ([]domain.Engagement, error) {
return s.engagementRepository.GetUserEngagement(userId, limit)
}