-
Notifications
You must be signed in to change notification settings - Fork 762
/
profile_data_store.go
60 lines (48 loc) · 1.29 KB
/
profile_data_store.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
// Copyright 2024 Daytona Platforms Inc.
// SPDX-License-Identifier: Apache-2.0
package db
import (
. "github.com/daytonaio/daytona/pkg/db/dto"
"github.com/daytonaio/daytona/pkg/profiledata"
"gorm.io/gorm"
)
type ProfileDataStore struct {
db *gorm.DB
}
func NewProfileDataStore(db *gorm.DB) (*ProfileDataStore, error) {
err := db.AutoMigrate(&ProfileDataDTO{})
if err != nil {
return nil, err
}
return &ProfileDataStore{db: db}, nil
}
func (p *ProfileDataStore) Get() (*profiledata.ProfileData, error) {
profileDataDTO := ProfileDataDTO{}
tx := p.db.Where("id = ?", ProfileDataId).First(&profileDataDTO)
if tx.Error != nil {
if tx.Error == gorm.ErrRecordNotFound {
return nil, profiledata.ErrProfileDataNotFound
}
return nil, tx.Error
}
profileData := ToProfileData(profileDataDTO)
return profileData, nil
}
func (p *ProfileDataStore) Save(profileData *profiledata.ProfileData) error {
profileDataDTO := ToProfileDataDTO(profileData)
tx := p.db.Save(&profileDataDTO)
if tx.Error != nil {
return tx.Error
}
return nil
}
func (p *ProfileDataStore) Delete() error {
tx := p.db.Where("id = ?", ProfileDataId).Delete(&ProfileDataDTO{})
if tx.Error != nil {
if tx.Error == gorm.ErrRecordNotFound {
return profiledata.ErrProfileDataNotFound
}
return tx.Error
}
return nil
}