Skip to content
Snippets Groups Projects
Commit b740ef4c authored by Sercan Yesildal's avatar Sercan Yesildal
Browse files

after find calculations added for Campaigns

parent cf5a3a64
No related branches found
No related tags found
No related merge requests found
package model
import "gorm.io/gorm"
type Campaign struct {
ID uint `json:"id" gorm:"primaryKey"`
Name string `json:"name" gorm:"notNull"`
......@@ -10,3 +12,13 @@ type Campaign struct {
Account Account `json:"account" gorm:"embedded; embeddedPrefix:account_"`
Donations []Donation `json:"donations" gorm:"foreignKey: CampaignID; constraint:OnUpdate:CASCADE, OnDelete:CASCADE"`
}
func (c *Campaign) AfterFind(tx *gorm.DB) (err error) {
var sum float64
result := tx.Model(&Donation{}).Select("ifnull(sum(amount),0)").Where("campaign_id = ?", c.ID).Scan(&sum)
if result.Error != nil {
return result.Error
}
c.AmountDonatedSoFar = sum
return nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment