init push
This commit is contained in:
60
internal/repository/mysql/flight_inspection_repo.go
Normal file
60
internal/repository/mysql/flight_inspection_repo.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
flightinspection "wucher/internal/domain/flight_inspection"
|
||||
)
|
||||
|
||||
type FlightInspectionRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewFlightInspectionRepository(db *gorm.DB) *FlightInspectionRepository {
|
||||
return &FlightInspectionRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *FlightInspectionRepository) Create(ctx context.Context, row *flightinspection.FlightInspection) error {
|
||||
return r.db.WithContext(ctx).Create(row).Error
|
||||
}
|
||||
|
||||
func (r *FlightInspectionRepository) Update(ctx context.Context, row *flightinspection.FlightInspection) error {
|
||||
return r.db.WithContext(ctx).Save(row).Error
|
||||
}
|
||||
|
||||
func (r *FlightInspectionRepository) GetByID(ctx context.Context, id []byte) (*flightinspection.FlightInspection, error) {
|
||||
var row flightinspection.FlightInspection
|
||||
err := r.db.WithContext(ctx).Where("id = ?", id).Take(&row).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &row, nil
|
||||
}
|
||||
|
||||
func (r *FlightInspectionRepository) List(ctx context.Context, sort string, limit, offset int) ([]flightinspection.FlightInspection, int64, error) {
|
||||
rows := make([]flightinspection.FlightInspection, 0)
|
||||
var total int64
|
||||
|
||||
base := r.db.WithContext(ctx).Model(&flightinspection.FlightInspection{})
|
||||
if err := base.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
query := base
|
||||
if sort != "" {
|
||||
query = query.Order(sort)
|
||||
}
|
||||
if limit > 0 {
|
||||
query = query.Limit(limit).Offset(offset)
|
||||
}
|
||||
if err := query.Find(&rows).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return rows, total, nil
|
||||
}
|
||||
Reference in New Issue
Block a user