61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
|
|
package repository
|
||
|
|
|
||
|
|
import (
|
||
|
|
"accounting-app/internal/models"
|
||
|
|
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
// AnnouncementRepository handles database operations for announcements
|
||
|
|
type AnnouncementRepository struct {
|
||
|
|
db *gorm.DB
|
||
|
|
}
|
||
|
|
|
||
|
|
// NewAnnouncementRepository creates a new AnnouncementRepository instance
|
||
|
|
func NewAnnouncementRepository(db *gorm.DB) *AnnouncementRepository {
|
||
|
|
return &AnnouncementRepository{
|
||
|
|
db: db,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Create creates a new announcement
|
||
|
|
func (r *AnnouncementRepository) Create(announcement *models.Announcement) error {
|
||
|
|
return r.db.Create(announcement).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
// ListResult represents the result of a list operation
|
||
|
|
type AnnouncementListResult struct {
|
||
|
|
Announcements []models.Announcement
|
||
|
|
Total int64
|
||
|
|
Limit int
|
||
|
|
Offset int
|
||
|
|
}
|
||
|
|
|
||
|
|
// List returns a list of announcements with pagination
|
||
|
|
func (r *AnnouncementRepository) List(limit, offset int) (*AnnouncementListResult, error) {
|
||
|
|
var announcements []models.Announcement
|
||
|
|
var total int64
|
||
|
|
|
||
|
|
// Count total announcements
|
||
|
|
if err := r.db.Model(&models.Announcement{}).Count(&total).Error; err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// Fetch announcements with pagination
|
||
|
|
err := r.db.Order("created_at desc").
|
||
|
|
Limit(limit).
|
||
|
|
Offset(offset).
|
||
|
|
Find(&announcements).Error
|
||
|
|
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
return &AnnouncementListResult{
|
||
|
|
Announcements: announcements,
|
||
|
|
Total: total,
|
||
|
|
Limit: limit,
|
||
|
|
Offset: offset,
|
||
|
|
}, nil
|
||
|
|
}
|