item repository and api progress.
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Date [3]int
|
||||
|
||||
func ParseDate(s string) (Date, error) {
|
||||
date, err := time.ParseInLocation("2006-01-02", s, time.UTC)
|
||||
if err != nil {
|
||||
return [3]int{}, err
|
||||
}
|
||||
|
||||
y, m, d := date.Date()
|
||||
return Date{y, int(m), d}, nil
|
||||
}
|
||||
|
||||
func (d *Date) UnmarshalJSON(b []byte) error {
|
||||
var str string
|
||||
err := json.Unmarshal(b, &str)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*d, err = ParseDate(str)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Date) String() string {
|
||||
return fmt.Sprintf("%04d-%02d-%02d", d[0], d[1], d[2])
|
||||
}
|
||||
|
||||
func (d Date) MarshalJSON() ([]byte, error) {
|
||||
return []byte("\"" + d.String() + "\""), nil
|
||||
}
|
||||
|
||||
func (d Date) ToTime() time.Time {
|
||||
return time.Date(d[0], time.Month(d[1]), d[2], 0, 0, 0, 0, time.UTC)
|
||||
}
|
||||
+80
-10
@@ -3,15 +3,85 @@ package models
|
||||
import "time"
|
||||
|
||||
type Item struct {
|
||||
ID int
|
||||
ProjectRequirementID *int
|
||||
OwnerID string
|
||||
ID int `json:"id"`
|
||||
ScopeID int `json:"scopeId"`
|
||||
OwnerID string `json:"-"`
|
||||
ProjectID *int `json:"projectId,omitempty"`
|
||||
ProjectRequirementID *int `json:"projectRequirementId,omitempty"`
|
||||
|
||||
Name string
|
||||
Description string
|
||||
OwnerName string
|
||||
CreatedTime time.Time
|
||||
AcquiredTime *time.Time
|
||||
ScheduledDate *time.Time
|
||||
Stats []StatProgressEntry
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
CreatedTime time.Time `json:"createdTime"`
|
||||
AcquiredTime *time.Time `json:"acquiredTime"`
|
||||
ScheduledDate *Date `json:"scheduledDate"`
|
||||
|
||||
Stats []StatProgressEntry `json:"stats,omitempty"`
|
||||
Owner *ScopeMember `json:"owner,omitempty"`
|
||||
}
|
||||
|
||||
func (item *Item) ApplyUpdate(update ItemUpdate) {
|
||||
if update.ProjectRequirementID != nil {
|
||||
item.ProjectRequirementID = update.ProjectRequirementID
|
||||
}
|
||||
if update.OwnerID != nil {
|
||||
item.OwnerID = *update.OwnerID
|
||||
item.Owner = nil
|
||||
}
|
||||
if update.Name != nil {
|
||||
item.Name = *update.Name
|
||||
}
|
||||
if update.Description != nil {
|
||||
item.Description = *update.Description
|
||||
}
|
||||
if update.AcquiredTime != nil {
|
||||
item.AcquiredTime = update.AcquiredTime
|
||||
}
|
||||
if update.ScheduledDate != nil {
|
||||
item.ScheduledDate = update.ScheduledDate
|
||||
}
|
||||
if update.ClearScheduledDate {
|
||||
item.ScheduledDate = nil
|
||||
}
|
||||
if update.ClearProjectRequirementID {
|
||||
item.ProjectRequirementID = nil
|
||||
}
|
||||
if update.ClearAcquiredTime {
|
||||
item.AcquiredTime = nil
|
||||
}
|
||||
|
||||
deleteList := make([]int, 0, len(update.Stats))
|
||||
for i, stat := range update.Stats {
|
||||
if stat.Acquired == 0 && stat.Required == 0 {
|
||||
deleteList = append(deleteList, i-len(deleteList))
|
||||
}
|
||||
|
||||
found := false
|
||||
for j := range item.Stats {
|
||||
if item.Stats[j].ID == stat.ID {
|
||||
item.Stats[j].Required = stat.Required
|
||||
item.Stats[j].Acquired = stat.Acquired
|
||||
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
item.Stats = append(item.Stats, stat)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type ItemUpdate struct {
|
||||
ProjectRequirementID *int `json:"projectRequirementId"`
|
||||
OwnerID *string `json:"ownerId"`
|
||||
Name *string `json:"name"`
|
||||
Description *string `json:"description"`
|
||||
AcquiredTime *time.Time `json:"acquiredTime"`
|
||||
ScheduledDate *Date `json:"scheduledDate"`
|
||||
|
||||
Stats []StatProgressEntry `json:"stats"`
|
||||
|
||||
ClearProjectRequirementID bool `json:"clearProjectRequirementId"`
|
||||
ClearAcquiredTime bool `json:"clearAcquiredTime"`
|
||||
ClearScheduledDate bool `json:"clearScheduledDate"`
|
||||
}
|
||||
|
||||
@@ -11,5 +11,19 @@ type ProjectRequirement struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Status Status `json:"status"`
|
||||
Stats []StatProgressEntry `json:"stats,omitempty"`
|
||||
Items []Item `json:"items,omitempty"`
|
||||
}
|
||||
|
||||
type ProjectRequirementUpdate struct {
|
||||
Name *string `json:"name"`
|
||||
Description *string `json:"description"`
|
||||
Status *Status `json:"status"`
|
||||
Stats []StatProgressEntry `json:"stats"`
|
||||
}
|
||||
|
||||
type Project struct {
|
||||
ProjectEntry
|
||||
Description string
|
||||
Requirements []ProjectRequirement
|
||||
}
|
||||
|
||||
@@ -16,9 +16,19 @@ type Scope struct {
|
||||
ScopeEntry
|
||||
|
||||
DisplayName string `json:"displayName"`
|
||||
Members []ScopeMember `json:"members"`
|
||||
Projects []ProjectEntry `json:"projects"`
|
||||
Stats []Stat `json:"stats"`
|
||||
Members []ScopeMember `json:"members,omitempty"`
|
||||
Projects []ProjectEntry `json:"projects,omitempty"`
|
||||
Stats []Stat `json:"stats,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Scope) Member(id string) *ScopeMember {
|
||||
for _, user := range s.Members {
|
||||
if user.ID == id {
|
||||
return &user
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Scope) HasMember(id string) bool {
|
||||
@@ -30,3 +40,13 @@ func (s *Scope) HasMember(id string) bool {
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *Scope) Stat(id int) *Stat {
|
||||
for _, stat := range s.Stats {
|
||||
if stat.ID == id {
|
||||
return &stat
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -19,3 +19,20 @@ type Stat struct {
|
||||
Description string `json:"description"`
|
||||
AllowedAmounts map[string]int `json:"allowedAmounts"`
|
||||
}
|
||||
|
||||
func (stat *Stat) AllowsAmount(amount int) bool {
|
||||
if stat == nil {
|
||||
return false
|
||||
}
|
||||
if stat.AllowedAmounts == nil || len(stat.AllowedAmounts) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
for _, v := range stat.AllowedAmounts {
|
||||
if v == amount {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user