f1909da1ad
Go CI / build (push) Failing after 2m42s
feat: create basic server to manage google oauth, account, sessions, places, attributes and ratings.
67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package store
|
|
|
|
import "git.pengzhan.dev/noteplace-server/internal/models"
|
|
|
|
// CreateRating adds a new rating to the store.
|
|
func (s *Store) CreateRating(rating models.Rating) error {
|
|
s.Mu.Lock()
|
|
defer s.Mu.Unlock()
|
|
s.Data.Ratings[rating.ID] = rating
|
|
return s.save()
|
|
}
|
|
|
|
// GetRatingByID finds a rating by its ID.
|
|
func (s *Store) GetRatingByID(id string) (models.Rating, bool) {
|
|
s.Mu.RLock()
|
|
defer s.Mu.RUnlock()
|
|
r, ok := s.Data.Ratings[id]
|
|
return r, ok
|
|
}
|
|
|
|
// UpdateRating finds a rating by ID and updates its score and comment.
|
|
func (s *Store) UpdateRating(id string, score int, comment string) (models.Rating, bool) {
|
|
s.Mu.Lock()
|
|
defer s.Mu.Unlock()
|
|
or, ok := s.Data.Ratings[id]
|
|
if !ok {
|
|
return models.Rating{}, false
|
|
}
|
|
s.Data.Ratings[id] = models.Rating{
|
|
ID: id,
|
|
PlaceID: or.PlaceID,
|
|
AttributeID: or.AttributeID,
|
|
UserID: or.UserID,
|
|
Score: score,
|
|
Comment: comment,
|
|
CreatedAt: or.CreatedAt,
|
|
}
|
|
if err := s.save(); err != nil {
|
|
return models.Rating{}, false
|
|
}
|
|
return s.Data.Ratings[id], true
|
|
}
|
|
|
|
// DeleteRating removes a rating from the store by its ID.
|
|
func (s *Store) DeleteRating(id string) bool {
|
|
s.Mu.Lock()
|
|
defer s.Mu.Unlock()
|
|
delete(s.Data.Ratings, id)
|
|
if err := s.save(); err != nil {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// GetRatingsByUserID finds all ratings for a given user ID.
|
|
func (s *Store) GetRatingsByUserID(userID string) []models.Rating {
|
|
s.Mu.RLock()
|
|
defer s.Mu.RUnlock()
|
|
var results []models.Rating
|
|
for _, r := range s.Data.Ratings {
|
|
if r.UserID == userID {
|
|
results = append(results, r)
|
|
}
|
|
}
|
|
return results
|
|
}
|