Add attendance_list in service_event, add set user permission in user
update Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
122
data/agenda.go
122
data/agenda.go
@@ -1,13 +1,119 @@
|
||||
package data
|
||||
|
||||
import "github.com/google/uuid"
|
||||
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"`
|
||||
EventId uuid.UUID `json:"event_id" gorm:"type:uuid;uniqueIndex:unique_event_user;not null"`
|
||||
UserId uuid.UUID `json:"user_id" gorm:"type:uuid;uniqueIndex:unique_event_user;not null"`
|
||||
Name string `json:"name" gorm:"type:varchar(255);not null"`
|
||||
Description string `json:"description" gorm:"type:text;not null"`
|
||||
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"`
|
||||
}
|
||||
|
||||
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) 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
|
||||
})
|
||||
}
|
||||
|
||||
@@ -112,23 +112,6 @@ func (self *Attendance) GetEventsByUserID(ctx context.Context, userID uuid.UUID)
|
||||
return &result, err
|
||||
}
|
||||
|
||||
func (self *Attendance) GetAttendanceByEventIdAndUserId(ctx context.Context, eventId, userId uuid.UUID) (*Attendance, error) {
|
||||
var attendance Attendance
|
||||
|
||||
err := Database.WithContext(ctx).
|
||||
Where("event_id = ? AND user_id = ?", eventId, userId).
|
||||
First(&attendance).Error
|
||||
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &attendance, nil
|
||||
}
|
||||
|
||||
func (self *Attendance) Create(ctx context.Context) error {
|
||||
self.UUID = uuid.New()
|
||||
self.AttendanceId = uuid.New()
|
||||
@@ -147,6 +130,17 @@ func (self *Attendance) Create(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *Attendance) GetAttendanceListByEventId(ctx context.Context, eventId uuid.UUID) (*[]Attendance, error) {
|
||||
var result []Attendance
|
||||
|
||||
err := Database.WithContext(ctx).
|
||||
Where("event_id = ?", eventId).
|
||||
Order("checkin_at DESC").
|
||||
Find(&result).Error
|
||||
|
||||
return &result, err
|
||||
}
|
||||
|
||||
func (self *Attendance) Update(ctx context.Context, attendanceId uuid.UUID) (*Attendance, error) {
|
||||
var attendance Attendance
|
||||
|
||||
|
||||
Reference in New Issue
Block a user