github.com/shoshinnikita/budget-manager@v0.7.1-0.20220131195411-8c46ff1c6778/internal/db/base/day.go (about)

     1  package base
     2  
     3  import (
     4  	"time"
     5  
     6  	common "github.com/ShoshinNikita/budget-manager/internal/db"
     7  	"github.com/ShoshinNikita/budget-manager/internal/pkg/money"
     8  )
     9  
    10  type Day struct {
    11  	ID      uint        `db:"id"`
    12  	MonthID uint        `db:"month_id"`
    13  	Day     int         `db:"day"`
    14  	Saldo   money.Money `db:"saldo"` // DailyBudget - Cost of all Spends
    15  
    16  	Spends []Spend `db:"-"`
    17  }
    18  
    19  // ToCommon converts Day to common Day structure from
    20  // "github.com/ShoshinNikita/budget-manager/internal/db" package
    21  func (d Day) ToCommon(year int, month time.Month) common.Day {
    22  	return common.Day{
    23  		ID:    d.ID,
    24  		Year:  year,
    25  		Month: month,
    26  		Day:   d.Day,
    27  		Saldo: d.Saldo,
    28  		Spends: func() []common.Spend {
    29  			spends := make([]common.Spend, 0, len(d.Spends))
    30  			for i := range d.Spends {
    31  				spends = append(spends, d.Spends[i].ToCommon(year, month, d.Day))
    32  			}
    33  			return spends
    34  		}(),
    35  	}
    36  }