/
repo.go
77 lines (60 loc) · 2.41 KB
/
repo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package location
import (
"github.com/eibrahimarisoy/car_rental/internal/models"
"github.com/google/uuid"
"go.uber.org/zap"
"github.com/eibrahimarisoy/car_rental/pkg/errorHandler"
pgHelper "github.com/eibrahimarisoy/car_rental/pkg/pagination"
"gorm.io/gorm"
)
type LocationRepository struct {
db *gorm.DB
}
//go:generate mockgen -destination=../../mocks/location/location_repository_interface.go -package=location github.com/eibrahimarisoy/car_rental/internal/location LocationRepositoryInterface
type LocationRepositoryInterface interface {
GetAllActiveLocations(pg *pgHelper.Pagination) (*[]models.Location, error)
CreateLocation(location *models.Location) (*models.Location, error)
GetLocationByID(id uuid.UUID) (*models.Location, error)
}
// NewLocationRepository creates a new location repository
func NewLocationRepository(db *gorm.DB) *LocationRepository {
return &LocationRepository{
db: db,
}
}
// Migration for location table
func (r *LocationRepository) Migration() {
r.db.AutoMigrate(&models.Location{})
}
// GetAllActiveLocations gets all active locations from database
func (r *LocationRepository) GetAllActiveLocations(pg *pgHelper.Pagination) (*[]models.Location, error) {
zap.L().Debug("location.repo.GetAllActiveLocations", zap.Reflect("pg", *pg))
var locations *[]models.Location
var totalRows int64
query := r.db.Model(&models.Location{}).Where("is_active = ?", true).Scopes(Search(pg.Q)).Count(&totalRows)
query.Scopes(pgHelper.Paginate(totalRows, pg, r.db)).Find(&locations)
if query.Error != nil {
return nil, query.Error
}
return locations, nil
}
// CreateLocation creates a location and returns it
func (r *LocationRepository) CreateLocation(location *models.Location) (*models.Location, error) {
zap.L().Debug("location.repo.CreateLocation", zap.Reflect("location", *location))
if err := r.db.Create(location).Error; err != nil {
return nil, err
}
return location, nil
}
// GetLocationByID gets location from database by id
func (r *LocationRepository) GetLocationByID(id uuid.UUID) (*models.Location, error) {
zap.L().Debug("location.repo.GetLocationByID", zap.Reflect("id", id))
var location models.Location
query := r.db.Model(&models.Location{}).Where("is_active = ? AND id = ?", true, id).First(&location)
if query.Error == gorm.ErrRecordNotFound {
return nil, errorHandler.LocationNotFoundError
} else if query.Error != nil {
return nil, query.Error
}
return &location, nil
}