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 }