lots of stuff, don't truly remember
This commit is contained in:
194
internal/event/handler.go
Normal file
194
internal/event/handler.go
Normal file
@@ -0,0 +1,194 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"rideaware/internal/config"
|
||||
"rideaware/internal/middleware"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
service *Service
|
||||
}
|
||||
|
||||
func NewHandler() *Handler {
|
||||
return &Handler{service: NewService()}
|
||||
}
|
||||
|
||||
func (h *Handler) CreateEvent(w http.ResponseWriter, r *http.Request) {
|
||||
claims := r.Context().Value(middleware.UserContextKey).(*config.CustomClaims)
|
||||
|
||||
var req struct {
|
||||
Name string `json:"name"`
|
||||
EventDate string `json:"event_date"`
|
||||
EventType string `json:"event_type"`
|
||||
Distance float64 `json:"distance"`
|
||||
Priority string `json:"priority"`
|
||||
Location string `json:"location"`
|
||||
Notes string `json:"notes"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": "invalid request"})
|
||||
return
|
||||
}
|
||||
|
||||
eventDate, err := time.Parse("2006-01-02", req.EventDate)
|
||||
if err != nil {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": "invalid event_date format (use YYYY-MM-DD)"})
|
||||
return
|
||||
}
|
||||
|
||||
event, err := h.service.CreateEvent(
|
||||
claims.UserID, req.Name, eventDate, req.EventType,
|
||||
req.Priority, req.Location, req.Notes, req.URL, req.Distance,
|
||||
)
|
||||
if err != nil {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(event)
|
||||
}
|
||||
|
||||
func (h *Handler) GetEvents(w http.ResponseWriter, r *http.Request) {
|
||||
claims := r.Context().Value(middleware.UserContextKey).(*config.CustomClaims)
|
||||
|
||||
events, err := h.service.GetUserEvents(claims.UserID)
|
||||
if err != nil {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": "failed to fetch events"})
|
||||
return
|
||||
}
|
||||
|
||||
if events == nil {
|
||||
events = []Event{}
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(events)
|
||||
}
|
||||
|
||||
func (h *Handler) GetUpcomingEvents(w http.ResponseWriter, r *http.Request) {
|
||||
claims := r.Context().Value(middleware.UserContextKey).(*config.CustomClaims)
|
||||
|
||||
events, err := h.service.GetUpcomingEvents(claims.UserID, 10)
|
||||
if err != nil {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": "failed to fetch events"})
|
||||
return
|
||||
}
|
||||
|
||||
if events == nil {
|
||||
events = []Event{}
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(events)
|
||||
}
|
||||
|
||||
func (h *Handler) UpdateEvent(w http.ResponseWriter, r *http.Request) {
|
||||
claims := r.Context().Value(middleware.UserContextKey).(*config.CustomClaims)
|
||||
|
||||
idStr := r.URL.Query().Get("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": "invalid event id"})
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Name string `json:"name"`
|
||||
EventDate string `json:"event_date"`
|
||||
EventType string `json:"event_type"`
|
||||
Distance float64 `json:"distance"`
|
||||
Priority string `json:"priority"`
|
||||
Location string `json:"location"`
|
||||
Notes string `json:"notes"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": "invalid request"})
|
||||
return
|
||||
}
|
||||
|
||||
var eventDate time.Time
|
||||
if req.EventDate != "" {
|
||||
eventDate, err = time.Parse("2006-01-02", req.EventDate)
|
||||
if err != nil {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": "invalid event_date format"})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
event, err := h.service.UpdateEvent(
|
||||
uint(id), claims.UserID, req.Name, eventDate, req.EventType,
|
||||
req.Priority, req.Location, req.Notes, req.URL, req.Distance,
|
||||
)
|
||||
if err != nil {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(event)
|
||||
}
|
||||
|
||||
func (h *Handler) DeleteEvent(w http.ResponseWriter, r *http.Request) {
|
||||
claims := r.Context().Value(middleware.UserContextKey).(*config.CustomClaims)
|
||||
|
||||
idStr := r.URL.Query().Get("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": "invalid event id"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.service.DeleteEvent(uint(id), claims.UserID); err != nil {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": "failed to delete event"})
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]string{"message": "event deleted"})
|
||||
}
|
||||
|
||||
func (h *Handler) GetEventTypes(w http.ResponseWriter, r *http.Request) {
|
||||
types := []map[string]interface{}{
|
||||
{"id": "race", "name": "Race", "icon": "\U0001F3C1", "color": "#FF1744"},
|
||||
{"id": "gran_fondo", "name": "Gran Fondo", "icon": "\U0001F3C6", "color": "#FF6D00"},
|
||||
{"id": "group_ride", "name": "Group Ride", "icon": "\U0001F6B4", "color": "#2979FF"},
|
||||
{"id": "time_trial", "name": "Time Trial", "icon": "\u23F1\uFE0F", "color": "#AA00FF"},
|
||||
{"id": "century", "name": "Century", "icon": "\U0001F5FA\uFE0F", "color": "#00C853"},
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(types)
|
||||
}
|
||||
18
internal/event/model.go
Normal file
18
internal/event/model.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package event
|
||||
|
||||
import "time"
|
||||
|
||||
type Event struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
UserID uint `gorm:"not null;index" json:"user_id"`
|
||||
Name string `gorm:"not null" json:"name"`
|
||||
EventDate time.Time `gorm:"not null;index" json:"event_date"`
|
||||
EventType string `gorm:"not null" json:"event_type"`
|
||||
Distance float64 `gorm:"default:0" json:"distance"`
|
||||
Priority string `gorm:"default:'C'" json:"priority"`
|
||||
Location string `gorm:"default:''" json:"location"`
|
||||
Notes string `gorm:"default:''" json:"notes"`
|
||||
URL string `gorm:"default:''" json:"url"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
65
internal/event/repository.go
Normal file
65
internal/event/repository.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"rideaware/pkg/database"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Repository struct{}
|
||||
|
||||
func NewRepository() *Repository {
|
||||
return &Repository{}
|
||||
}
|
||||
|
||||
func (r *Repository) CreateEvent(event *Event) error {
|
||||
return database.DB.Create(event).Error
|
||||
}
|
||||
|
||||
func (r *Repository) GetEventByID(id, userID uint) (*Event, error) {
|
||||
var event Event
|
||||
if err := database.DB.Where("id = ? AND user_id = ?", id, userID).First(&event).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errors.New("event not found")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &event, nil
|
||||
}
|
||||
|
||||
func (r *Repository) GetUserEvents(userID uint) ([]Event, error) {
|
||||
var events []Event
|
||||
if err := database.DB.Where("user_id = ?", userID).Order("event_date ASC").Find(&events).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func (r *Repository) GetEventsByDateRange(userID uint, start, end time.Time) ([]Event, error) {
|
||||
var events []Event
|
||||
if err := database.DB.Where("user_id = ? AND event_date BETWEEN ? AND ?", userID, start, end).
|
||||
Order("event_date ASC").Find(&events).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func (r *Repository) GetUpcomingEvents(userID uint, limit int) ([]Event, error) {
|
||||
var events []Event
|
||||
if err := database.DB.Where("user_id = ? AND event_date >= ?", userID, time.Now()).
|
||||
Order("event_date ASC").Limit(limit).Find(&events).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func (r *Repository) UpdateEvent(event *Event) error {
|
||||
return database.DB.Save(event).Error
|
||||
}
|
||||
|
||||
func (r *Repository) DeleteEvent(id, userID uint) error {
|
||||
return database.DB.Where("id = ? AND user_id = ?", id, userID).Delete(&Event{}).Error
|
||||
}
|
||||
105
internal/event/service.go
Normal file
105
internal/event/service.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
repo *Repository
|
||||
}
|
||||
|
||||
func NewService() *Service {
|
||||
return &Service{repo: NewRepository()}
|
||||
}
|
||||
|
||||
func (s *Service) CreateEvent(userID uint, name string, eventDate time.Time, eventType, priority, location, notes, url string, distance float64) (*Event, error) {
|
||||
if name == "" {
|
||||
return nil, errors.New("name is required")
|
||||
}
|
||||
if eventDate.IsZero() {
|
||||
return nil, errors.New("event_date is required")
|
||||
}
|
||||
if !isValidEventType(eventType) {
|
||||
return nil, errors.New("invalid event_type")
|
||||
}
|
||||
if !isValidPriority(priority) {
|
||||
priority = "C"
|
||||
}
|
||||
|
||||
event := &Event{
|
||||
UserID: userID,
|
||||
Name: name,
|
||||
EventDate: eventDate,
|
||||
EventType: eventType,
|
||||
Distance: distance,
|
||||
Priority: priority,
|
||||
Location: location,
|
||||
Notes: notes,
|
||||
URL: url,
|
||||
}
|
||||
|
||||
if err := s.repo.CreateEvent(event); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return event, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetUserEvents(userID uint) ([]Event, error) {
|
||||
return s.repo.GetUserEvents(userID)
|
||||
}
|
||||
|
||||
func (s *Service) GetUpcomingEvents(userID uint, limit int) ([]Event, error) {
|
||||
return s.repo.GetUpcomingEvents(userID, limit)
|
||||
}
|
||||
|
||||
func (s *Service) UpdateEvent(id, userID uint, name string, eventDate time.Time, eventType, priority, location, notes, url string, distance float64) (*Event, error) {
|
||||
event, err := s.repo.GetEventByID(id, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if name != "" {
|
||||
event.Name = name
|
||||
}
|
||||
if !eventDate.IsZero() {
|
||||
event.EventDate = eventDate
|
||||
}
|
||||
if eventType != "" {
|
||||
if !isValidEventType(eventType) {
|
||||
return nil, errors.New("invalid event_type")
|
||||
}
|
||||
event.EventType = eventType
|
||||
}
|
||||
if priority != "" {
|
||||
if !isValidPriority(priority) {
|
||||
return nil, errors.New("invalid priority")
|
||||
}
|
||||
event.Priority = priority
|
||||
}
|
||||
event.Location = location
|
||||
event.Notes = notes
|
||||
event.URL = url
|
||||
event.Distance = distance
|
||||
|
||||
if err := s.repo.UpdateEvent(event); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return event, nil
|
||||
}
|
||||
|
||||
func (s *Service) DeleteEvent(id, userID uint) error {
|
||||
return s.repo.DeleteEvent(id, userID)
|
||||
}
|
||||
|
||||
func isValidEventType(t string) bool {
|
||||
valid := map[string]bool{
|
||||
"race": true, "gran_fondo": true, "group_ride": true,
|
||||
"time_trial": true, "century": true,
|
||||
}
|
||||
return valid[t]
|
||||
}
|
||||
|
||||
func isValidPriority(p string) bool {
|
||||
return p == "A" || p == "B" || p == "C"
|
||||
}
|
||||
Reference in New Issue
Block a user