-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
110 lines (87 loc) · 2.55 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"net/http"
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/rlanier-webdev/RivalryAPIv2/models"
"gorm.io/gorm"
)
// Game Handlers
func getGamesHandler(c *gin.Context) {
var games []models.Game
if err := db.Find(&games).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(200, games)
}
func getGameByIDHandler(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil || id <= 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid game ID"})
return
}
var game models.Game
if err := db.First(&game, id).Error; err != nil {
if err == gorm.ErrRecordNotFound {
c.JSON(http.StatusNotFound, gin.H{"error": "Game not found"})
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
return
}
c.JSON(http.StatusOK, game)
}
func getGamesByYearHandler(c *gin.Context) {
yearStr := c.Param("year")
year, err := strconv.Atoi(yearStr)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid year"})
return
}
startDate := time.Date(year, time.January, 1, 0, 0, 0, 0, time.UTC)
endDate := time.Date(year+1, time.January, 1, 0, 0, 0, 0, time.UTC)
var games []models.Game
if err := db.Where("date >= ? AND date < ?", startDate, endDate).Find(&games).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, games)
}
func getGamesByHomeHandler(c *gin.Context) {
homeTeam := c.Param("team")
if homeTeam == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Team name cannot be empty"})
return
}
var games []models.Game
if err := db.Where("home_team = ?", homeTeam).Find(&games).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrive games: " + err.Error()})
return
}
if len(games) == 0 {
c.JSON(http.StatusNotFound, gin.H{"message": "No games found for the given team"})
return
}
c.JSON(http.StatusOK, games)
}
func getGamesByAwayHandler(c *gin.Context) {
homeTeam := c.Param("team")
if homeTeam == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Team name cannot be empty"})
return
}
var games []models.Game
if err := db.Where("away_team = ?", homeTeam).Find(&games).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrive games: " + err.Error()})
return
}
if len(games) == 0 {
c.JSON(http.StatusNotFound, gin.H{"message": "No games found for the given team"})
return
}
c.JSON(http.StatusOK, games)
}
// End game handlers