예제 #1
0
func (repo *DbAccountRepo) FindById(id, userId int) (*domain.Account, error) {
	rt := new(domain.Account)
	card := new(domain.CreditCard)
	err := repo.db.QueryRow("SELECT a.id, account_type, account_name, starting_amount, at.name, display_order, COALESCE(comment, ''), COALESCE(c.last_date, 0), COALESCE(c.paying_month, 0), COALESCE(c.paying_day, 0), COALESCE(c.paying_account, 0), COALESCE(c.holiday, 0) FROM accounts AS a INNER JOIN account_types AS at ON (at.id = a.account_type) LEFT OUTER JOIN credit_cards AS c ON (c.account_id = a.id) WHERE a.id = $1 AND a.owner_id = $2", id, userId).Scan(&rt.Id, &rt.Type, &rt.Name, &rt.StartingAmount, &rt.TypeName, &rt.Order, &rt.Comment, &card.LastDate, &card.PayingMonth, &card.PayingDay, &card.PayingAccount, &card.Holiday)
	if err != nil {
		return nil, err
	}
	if rt.Type == 4 {
		rt.CreditCard = card
	}
	return rt, nil
}
예제 #2
0
func (repo *DbAccountRepo) FindAllByOwner(userId int) ([]*domain.Account, error) {
	rows, err := repo.db.Query("SELECT a.id, account_type, account_name, starting_amount, at.name, display_order, COALESCE(c.last_date, 0), COALESCE(c.paying_month, 0), COALESCE(c.paying_day, 0), COALESCE(c.paying_account, 0), COALESCE(c.holiday, 0) FROM accounts AS a INNER JOIN account_types AS at ON (at.id = a.account_type) LEFT OUTER JOIN credit_cards AS c ON (c.account_id = a.id) WHERE a.owner_id = $1 ORDER BY display_order ASC, id ASC", userId)
	if err != nil {
		return nil, err
	}

	var rt []*domain.Account
	for rows.Next() {
		account := new(domain.Account)
		card := new(domain.CreditCard)
		err = rows.Scan(&account.Id, &account.Type, &account.Name, &account.StartingAmount, &account.TypeName, &account.Order, &card.LastDate, &card.PayingMonth, &card.PayingDay, &card.PayingAccount, &card.Holiday)
		if err != nil {
			return nil, err
		}
		if account.Type == 4 {
			account.CreditCard = card
		}
		rt = append(rt, account)
	}
	return rt, nil
}
예제 #3
0
func (handler AccountHandler) PutPostJSON(w http.ResponseWriter, r *http.Request, user *usecases.User, title *Title) *WebError {
	vars := mux.Vars(r)
	var err error
	account := new(domain.Account)
	if vars["id"] != "create" {
		id, _ := strconv.Atoi(vars["id"])
		account, err = handler.AccountInteractor.FetchById(id, user.Id)
		if err != nil {
			log.Println(err)
			return nil
		}
	}
	rt, err := ParseJSONBody(r)
	if err != nil {
		log.Println(err)
		return nil
	}
	if name, ok := rt["Name"].(string); ok {
		account.Name = name
	}
	account.Type = InterfaceToInt(rt["Type"], 0)
	account.StartingAmount = InterfaceToInt64(rt["StartingAmount"], 0)
	account.Order = InterfaceToInt(rt["Order"], account.Order)
	if comment, ok := rt["Comment"].(string); ok {
		account.Comment = comment
	}
	if rt["CreditCard"] != nil {
		if card, ok := rt["CreditCard"].(map[string]interface{}); ok {
			if account.CreditCard == nil {
				account.CreditCard = new(domain.CreditCard)
			}
			account.CreditCard.LastDate = InterfaceToInt(card["LastDate"], 0)
			account.CreditCard.PayingMonth = InterfaceToInt(card["PayingMonth"], 0)
			account.CreditCard.PayingDay = InterfaceToInt(card["PayingDay"], 0)
			account.CreditCard.PayingAccount = InterfaceToInt(card["PayingAccount"], 0)
			account.CreditCard.Holiday = InterfaceToInt(card["Holiday"], 0)
		}
	}
	account.OwnerId = user.Id
	err = handler.AccountInteractor.Save(account)
	if err != nil {
		return &WebError{
			Error:   err,
			Code:    500,
			Message: "Error saving account",
			Json:    true,
		}
	}
	return nil
}