func UpdateOrgQuota(cmd *m.UpdateOrgQuotaCmd) error { return inTransaction2(func(sess *session) error { //Check if quota is already defined in the DB quota := m.Quota{ Target: cmd.Target, OrgId: cmd.OrgId, } has, err := sess.Get("a) if err != nil { return err } quota.Limit = cmd.Limit if has == false { //No quota in the DB for this target, so create a new one. if _, err := sess.Insert("a); err != nil { return err } } else { //update existing quota entry in the DB. if _, err := sess.Id(quota.Id).Update("a); err != nil { return err } } return nil }) }
func GetOrgQuotaByTarget(query *m.GetOrgQuotaByTargetQuery) error { quota := m.Quota{ Target: query.Target, OrgId: query.OrgId, } has, err := x.Get("a) if err != nil { return err } else if has == false { quota.Limit = query.Default } //get quota used. rawSql := fmt.Sprintf("SELECT COUNT(*) as count from %s where org_id=?", dialect.Quote(query.Target)) resp := make([]*targetCount, 0) if err := x.Sql(rawSql, query.OrgId).Find(&resp); err != nil { return err } query.Result = &m.OrgQuotaDTO{ Target: query.Target, Limit: quota.Limit, OrgId: query.OrgId, Used: resp[0].Count, } return nil }