45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package repository
|
|
|
|
import (
|
|
"fmt"
|
|
"heating-monitor/internal/bot"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// GetEventByID obtiene un evento de la base de datos por su ID
|
|
func GetEventByID(db *gorm.DB, id int) (*bot.HeatingEvent, error) {
|
|
var event bot.HeatingEvent
|
|
if err := db.First(&event, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &event, nil
|
|
}
|
|
|
|
// UpdateEventByID actualiza un evento por su ID
|
|
func UpdateEventByID(db *gorm.DB, id int, updatedEvent *bot.HeatingEvent) (*bot.HeatingEvent, error) {
|
|
var event bot.HeatingEvent
|
|
if err := db.First(&event, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
// Actualizamos los campos relevantes del evento
|
|
event.EventType = updatedEvent.EventType
|
|
event.Timestamp = updatedEvent.Timestamp
|
|
if err := db.Save(&event).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &event, nil
|
|
}
|
|
|
|
// DeleteEventByID elimina un evento por su ID
|
|
func DeleteEventByID(db *gorm.DB, id int) error {
|
|
result := db.Delete(&bot.HeatingEvent{}, id)
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
if result.RowsAffected == 0 {
|
|
return fmt.Errorf("No se encontró un evento con el ID %d", id)
|
|
}
|
|
return nil
|
|
}
|