126 lines
2.8 KiB
Go
126 lines
2.8 KiB
Go
package data
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Agenda struct {
|
|
Id uint `json:"id" gorm:"primarykey;autoIncrement"`
|
|
UUID uuid.UUID `json:"uuid" gorm:"type:uuid;uniqueIndex;not null"`
|
|
AgendaId uuid.UUID `json:"agenda_id" gorm:"type:uuid;uniqueIndex;not null"`
|
|
AttendanceId uuid.UUID `json:"attendance_id" gorm:"type:uuid;not null"`
|
|
Name string `json:"name" gorm:"type:varchar(255);not null"`
|
|
Description string `json:"description" gorm:"type:text;not null"`
|
|
IsApproved bool `json:"is_approved" gorm:"type:boolean;default:false"`
|
|
}
|
|
|
|
func (self *Agenda) SetAttendanceId(id uuid.UUID) *Agenda {
|
|
self.AttendanceId = id
|
|
return self
|
|
}
|
|
|
|
func (self *Agenda) SetName(name string) *Agenda {
|
|
self.Name = name
|
|
return self
|
|
}
|
|
|
|
func (self *Agenda) SetDescription(desc string) *Agenda {
|
|
self.Description = desc
|
|
return self
|
|
}
|
|
|
|
func (self *Agenda) SetIsApproved(approved bool) *Agenda {
|
|
self.IsApproved = approved
|
|
return self
|
|
}
|
|
|
|
func (self *Agenda) GetByAgendaId(ctx context.Context, agendaId uuid.UUID) (*Agenda, error) {
|
|
var agenda Agenda
|
|
|
|
err := Database.WithContext(ctx).
|
|
Where("agenda_id = ?", agendaId).
|
|
First(&agenda).Error
|
|
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
return &agenda, nil
|
|
}
|
|
|
|
func (self *Agenda) GetListByAttendanceId(ctx context.Context, attendanceId uuid.UUID) (*[]Agenda, error) {
|
|
var result []Agenda
|
|
|
|
err := Database.WithContext(ctx).
|
|
Model(&Agenda{}).
|
|
Where("attendance_id = ?", attendanceId).
|
|
Order("id ASC").
|
|
Scan(&result).Error
|
|
|
|
return &result, err
|
|
}
|
|
|
|
func (self *Agenda) Create(ctx context.Context) error {
|
|
self.UUID = uuid.New()
|
|
self.AgendaId = uuid.New()
|
|
|
|
err := Database.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
if err := tx.Create(&self).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (self *Agenda) Update(ctx context.Context, agendaId uuid.UUID) (*Agenda, error) {
|
|
var agenda Agenda
|
|
|
|
err := Database.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
if err := tx.
|
|
Where("agenda_id = ?", agendaId).
|
|
First(&agenda).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := tx.
|
|
Model(&agenda).
|
|
Updates(self).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
return tx.
|
|
Where("agenda_id = ?", agendaId).
|
|
First(&agenda).Error
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &agenda, nil
|
|
}
|
|
|
|
func (self *Agenda) Delete(ctx context.Context, agendaId uuid.UUID) error {
|
|
return Database.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
result := tx.Where("agenda_id = ?", agendaId).Delete(&Agenda{})
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
if result.RowsAffected == 0 {
|
|
return gorm.ErrRecordNotFound
|
|
}
|
|
return nil
|
|
})
|
|
}
|