예제 #1
0
파일: user.go 프로젝트: nizsheanez/ria
func FindUsers() ([]User, error) {
	qb := squirrel.Select("*").From("user")

	var users []User
	err := db.LoadCollection(&qb, &users)
	if err != nil {
		return nil, err
	}

	return users, nil
}
예제 #2
0
파일: user.go 프로젝트: nizsheanez/ria
func (this *User) GetInitialData() (result map[string]interface{}, err error) {

	type ResponseGoal struct {
		Goal
		Today     *Day `json:"today"`
		Yesterday *Day `json:"yesterday"`
	}

	type Response struct {
		Categories  map[string]*GoalCategory `json:"categories"`
		Goals       []*ResponseGoal          `json:"goals"`
		Conclusions map[string]*Conclusion   `json:"conclusions"`
	}

	response := &Response{}

	var rawGoals []*Goal
	qb := squirrel.Select("*").
		From("goal").
		Where("fk_user = ?", this.Id)

	err = db.LoadCollection(&qb, &rawGoals)
	if err != nil {
		return nil, err
	}

	days := []string{"today", "yesterday"}

	response.Goals = make([]*ResponseGoal, len(rawGoals))
	for i, goal := range rawGoals {
		response.Goals[i] = &ResponseGoal{Goal: *goal}
		response.Goals[i].Today = &Day{Report: &Report{}}
		response.Goals[i].Yesterday = &Day{Report: &Report{}}
		for _, day := range days {
			var day1 time.Time
			if day == "today" {
				day1 = time.Now()
			} else {
				day1 = time.Now().AddDate(0, 0, -1)
			}
			day2 := day1.AddDate(0, 0, +1)

			qb = squirrel.Select("*").
				From("report").
				Where("report_date >= ?", FormatDate(day1)).
				Where("report_date < ?", FormatDate(day2)).
				Where("fk_goal = ?", response.Goals[i].Id)

			if day == "today" {
				err = db.LoadStruct(&qb, response.Goals[i].Today.Report)
				if err != nil {
					return nil, err
				}
				//TODO: create if not found
			} else {
				err = db.LoadStruct(&qb, response.Goals[i].Yesterday.Report)
				if err != nil {
					return nil, err
				}
				//TODO: create if not found
			}
		}
	}

	response.Conclusions = make(map[string]*Conclusion, len(days))

	for _, day := range days {
		response.Conclusions[day] = &Conclusion{}

		var day1 time.Time
		if day == "today" {
			day1 = time.Now()
		} else {
			day1 = time.Now().AddDate(0, 0, -1)
		}
		day2 := day1.AddDate(0, 0, +1)

		qb = squirrel.Select("*").
			From("conclusion").
			Where("report_date >= ?", FormatDate(day1)).
			Where("report_date < ?", FormatDate(day2))

		err = db.LoadStruct(&qb, response.Conclusions[day])
		if err != nil {
			return nil, err
		}

		//TODO: create if not found
	}

	var cats []*GoalCategory
	qb = squirrel.Select("*").
		From("goal_category")

	err = db.LoadCollection(&qb, &cats)
	if err != nil {
		return nil, err
	}
	response.Categories = make(map[string]*GoalCategory, len(cats))
	for _, cat := range cats {
		response.Categories[strconv.Itoa(cat.Id)] = cat
	}

	result = map[string]interface{}{
		"categories":  response.Categories,
		"goals":       response.Goals,
		"conclusions": response.Conclusions,
	}

	return result, nil
}