mirror of
https://github.com/Dvorinka/MyClubServer.git
synced 2026-06-04 10:42:57 +00:00
146 lines
4.5 KiB
Go
146 lines
4.5 KiB
Go
package controllers
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"fotbal-club/internal/services"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type AdminWeatherController struct {
|
|
weatherService *services.WeatherService
|
|
}
|
|
|
|
func NewAdminWeatherController(weatherService *services.WeatherService) *AdminWeatherController {
|
|
return &AdminWeatherController{
|
|
weatherService: weatherService,
|
|
}
|
|
}
|
|
|
|
// GetWeather returns weather information for the club location
|
|
func (wc *AdminWeatherController) GetWeather(c *gin.Context) {
|
|
// Get location from query parameter or use club location
|
|
location := c.Query("location")
|
|
|
|
weather, err := wc.weatherService.GetWeatherByLocation(location)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": "Failed to fetch weather data",
|
|
"details": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
// Transform the response to include absolute icon URLs
|
|
if weather.Current.Condition.Icon != "" {
|
|
weather.Current.Condition.Icon = wc.weatherService.GetWeatherIconURL(weather.Current.Condition.Icon)
|
|
}
|
|
|
|
// Update forecast day icons
|
|
for i := range weather.Forecast.ForecastDay {
|
|
if weather.Forecast.ForecastDay[i].Day.Condition.Icon != "" {
|
|
weather.Forecast.ForecastDay[i].Day.Condition.Icon = wc.weatherService.GetWeatherIconURL(weather.Forecast.ForecastDay[i].Day.Condition.Icon)
|
|
}
|
|
// Update hour icons
|
|
for j := range weather.Forecast.ForecastDay[i].Hour {
|
|
if weather.Forecast.ForecastDay[i].Hour[j].Condition.Icon != "" {
|
|
weather.Forecast.ForecastDay[i].Hour[j].Condition.Icon = wc.weatherService.GetWeatherIconURL(weather.Forecast.ForecastDay[i].Hour[j].Condition.Icon)
|
|
}
|
|
}
|
|
}
|
|
|
|
c.JSON(http.StatusOK, weather)
|
|
}
|
|
|
|
// GetWeatherForClub returns weather for the configured club location
|
|
func (wc *AdminWeatherController) GetWeatherForClub(c *gin.Context) {
|
|
weather, err := wc.weatherService.GetWeatherForClub()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": "Failed to fetch weather data for club",
|
|
"details": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
// Transform the response to include absolute icon URLs
|
|
if weather.Current.Condition.Icon != "" {
|
|
weather.Current.Condition.Icon = wc.weatherService.GetWeatherIconURL(weather.Current.Condition.Icon)
|
|
}
|
|
|
|
// Update forecast day icons
|
|
for i := range weather.Forecast.ForecastDay {
|
|
if weather.Forecast.ForecastDay[i].Day.Condition.Icon != "" {
|
|
weather.Forecast.ForecastDay[i].Day.Condition.Icon = wc.weatherService.GetWeatherIconURL(weather.Forecast.ForecastDay[i].Day.Condition.Icon)
|
|
}
|
|
}
|
|
|
|
c.JSON(http.StatusOK, weather)
|
|
}
|
|
|
|
// GetWeatherForMatch returns weather forecast for a specific match time and location
|
|
func (wc *AdminWeatherController) GetWeatherForMatch(c *gin.Context) {
|
|
// Get match datetime and location from query parameters
|
|
matchDateTime := c.Query("match_datetime")
|
|
location := c.Query("location")
|
|
|
|
if matchDateTime == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "match_datetime parameter is required",
|
|
})
|
|
return
|
|
}
|
|
|
|
weather, err := wc.weatherService.GetWeatherForMatch(matchDateTime, location)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": "Failed to fetch weather data for match",
|
|
"details": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
// Transform the response to include absolute icon URLs
|
|
if weather.Current.Condition.Icon != "" {
|
|
weather.Current.Condition.Icon = wc.weatherService.GetWeatherIconURL(weather.Current.Condition.Icon)
|
|
}
|
|
|
|
// Update forecast day icons
|
|
for i := range weather.Forecast.ForecastDay {
|
|
if weather.Forecast.ForecastDay[i].Day.Condition.Icon != "" {
|
|
weather.Forecast.ForecastDay[i].Day.Condition.Icon = wc.weatherService.GetWeatherIconURL(weather.Forecast.ForecastDay[i].Day.Condition.Icon)
|
|
}
|
|
// Update hour icons
|
|
for j := range weather.Forecast.ForecastDay[i].Hour {
|
|
if weather.Forecast.ForecastDay[i].Hour[j].Condition.Icon != "" {
|
|
weather.Forecast.ForecastDay[i].Hour[j].Condition.Icon = wc.weatherService.GetWeatherIconURL(weather.Forecast.ForecastDay[i].Hour[j].Condition.Icon)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Find the closest hourly forecast to the match time
|
|
matchTime, err := time.Parse("2006-01-02T15:04:05", matchDateTime)
|
|
if err != nil {
|
|
// Try alternative format
|
|
matchTime, err = time.Parse("2006-01-02 15:04:05", matchDateTime)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "Invalid match_datetime format",
|
|
})
|
|
return
|
|
}
|
|
}
|
|
|
|
closestHour := wc.weatherService.FindClosestHourlyForecast(weather, matchTime)
|
|
|
|
response := gin.H{
|
|
"weather": weather,
|
|
"match_time": matchDateTime,
|
|
"closest_hour": closestHour,
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response)
|
|
}
|