You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
3 years ago
|
package integration
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"time"
|
||
|
"caj-larsson/bog/domain"
|
||
|
)
|
||
|
|
||
|
type UserAgentDBRecord struct {
|
||
|
ID int64
|
||
|
Name string
|
||
|
LastSeen string
|
||
|
AllowanceSeconds int64
|
||
|
QuotaKB int64
|
||
|
QuotaUsedKB int64
|
||
|
}
|
||
|
|
||
|
var ErrUnparseableRecord = errors.New("record could not be mapped to entity")
|
||
|
|
||
|
func (r *UserAgentDBRecord) toEntity() (*domain.UserAgent, error) {
|
||
|
lastseen, err := time.Parse(time.RFC3339, r.LastSeen)
|
||
|
|
||
|
if err != nil {
|
||
|
return nil, ErrUnparseableRecord
|
||
|
}
|
||
|
|
||
|
var useragent = new(domain.UserAgent)
|
||
|
useragent.ID = r.ID
|
||
|
useragent.Name = r.Name
|
||
|
useragent.LastSeen = lastseen
|
||
|
useragent.AllowanceDuration = time.Duration(r.AllowanceSeconds * int64(time.Second))
|
||
|
useragent.FileQuota = domain.FileSizeQuota { r.QuotaKB, r.QuotaUsedKB }
|
||
|
return useragent, err
|
||
|
}
|
||
|
|
||
|
func fromEntity(useragent domain.UserAgent) (*UserAgentDBRecord, error) {
|
||
|
var record = new(UserAgentDBRecord)
|
||
|
record.ID = useragent.ID
|
||
|
record.Name = useragent.Name
|
||
|
record.LastSeen = useragent.LastSeen.Format(time.RFC3339)
|
||
|
record.AllowanceSeconds = int64(useragent.AllowanceDuration.Seconds())
|
||
|
record.QuotaKB = useragent.FileQuota.AllowanceKB
|
||
|
record.QuotaUsedKB = useragent.FileQuota.CurrentUsage
|
||
|
return record, nil
|
||
|
}
|