2014-03-25 03:31:33 +00:00
package models
import (
"database/sql"
"fmt"
"time"
)
//Campaign is a struct representing a created campaign
type Campaign struct {
Id int64 ` json:"id" `
2014-03-26 04:53:51 +00:00
UserId int64 ` json:"-" `
Name string ` json:"name" sql:"not null" `
CreatedDate time . Time ` json:"created_date" `
CompletedDate time . Time ` json:"completed_date" `
2014-03-25 03:31:33 +00:00
Template string ` json:"template" ` //This may change
Status string ` json:"status" `
2014-03-26 04:53:51 +00:00
Results [ ] Result ` json:"results,omitempty" `
Groups [ ] Group ` json:"groups,omitempty" `
2014-03-25 03:31:33 +00:00
}
type Result struct {
2014-03-26 04:53:51 +00:00
Id int64 ` json:"-" `
CampaignId int64 ` json:"-" `
Email string ` json:"email" `
Status string ` json:"status" sql:"not null" `
2014-03-25 03:31:33 +00:00
}
// GetCampaigns returns the campaigns owned by the given user.
func GetCampaigns ( uid int64 ) ( [ ] Campaign , error ) {
cs := [ ] Campaign { }
2014-03-26 04:53:51 +00:00
err := db . Debug ( ) . Model ( & User { Id : uid } ) . Related ( & cs ) . Error
if err != nil {
fmt . Println ( err )
}
/ * _ , err = Conn . Select ( & cs , "SELECT c.id, name, created_date, completed_date, status, template FROM campaigns c, user_campaigns uc, users u WHERE uc.uid=u.id AND uc.cid=c.id AND u.id=?" , uid )
* / for i , _ := range cs {
err := db . Debug ( ) . Model ( & cs [ i ] ) . Related ( & cs [ i ] . Results ) . Error
if err != nil {
fmt . Println ( err )
}
/ * _ , err = Conn . Select ( & cs [ i ] . Results , "SELECT r.email, r.status FROM campaign_results r WHERE r.cid=?" , cs [ i ] . Id )
* /
2014-03-25 03:31:33 +00:00
}
2014-03-26 04:53:51 +00:00
fmt . Printf ( "%v" , cs )
2014-03-25 03:31:33 +00:00
return cs , err
}
// GetCampaign returns the campaign, if it exists, specified by the given id and user_id.
func GetCampaign ( id int64 , uid int64 ) ( Campaign , error ) {
c := Campaign { }
2014-03-26 04:53:51 +00:00
err := db . Debug ( ) . Where ( "id = ?" , id ) . Where ( "user_id = ?" , uid ) . Find ( & c ) . Error
/ * err := Conn . SelectOne ( & c , "SELECT c.id, name, created_date, completed_date, status, template FROM campaigns c, user_campaigns uc, users u WHERE uc.uid=u.id AND uc.cid=c.id AND c.id=? AND u.id=?" , id , uid )
* / if err != nil {
2014-03-25 03:31:33 +00:00
return c , err
}
2014-03-26 04:53:51 +00:00
err = db . Debug ( ) . Model ( & c ) . Related ( & c . Results ) . Error
/ * _ , err = Conn . Select ( & c . Results , "SELECT r.email, r.status FROM campaign_results r WHERE r.cid=?" , c . Id )
* / return c , err
2014-03-25 03:31:33 +00:00
}
// PostCampaign inserts a campaign and all associated records into the database.
func PostCampaign ( c * Campaign , uid int64 ) error {
// Check to make sure all the groups already exist
for i , g := range c . Groups {
c . Groups [ i ] , err = GetGroupByName ( g . Name , uid )
if err == sql . ErrNoRows {
Logger . Printf ( "Error - Group %s does not exist" , g . Name )
return err
} else if err != nil {
Logger . Println ( err )
return err
}
}
// Insert into the DB
2014-03-26 04:53:51 +00:00
/*err = Conn.Insert(c)*/
err = db . Debug ( ) . Save ( & c ) . Error
2014-03-25 03:31:33 +00:00
if err != nil {
Logger . Println ( err )
return err
}
// Insert all the results
for _ , g := range c . Groups {
// Insert a result for each target in the group
for _ , t := range g . Targets {
2014-03-26 04:53:51 +00:00
r := Result { Email : t . Email , Status : "Unknown" , CampaignId : c . Id }
2014-03-25 03:31:33 +00:00
c . Results = append ( c . Results , r )
fmt . Printf ( "%v" , c . Results )
2014-03-26 04:53:51 +00:00
err := db . Debug ( ) . Save ( & r ) . Error
/*_, err = Conn.Exec("INSERT INTO campaign_results VALUES (?,?,?)", c.Id, r.Email, r.Status)*/
2014-03-25 03:31:33 +00:00
if err != nil {
Logger . Printf ( "Error adding result record for target %s\n" , t . Email )
Logger . Println ( err )
}
}
}
2014-03-26 04:53:51 +00:00
/ * _ , err = Conn . Exec ( "INSERT OR IGNORE INTO user_campaigns VALUES (?,?)" , uid , c . Id )
2014-03-25 03:31:33 +00:00
if err != nil {
Logger . Printf ( "Error adding many-many mapping for campaign %s\n" , c . Name )
2014-03-26 04:53:51 +00:00
} * /
2014-03-25 03:31:33 +00:00
return nil
}
func DeleteCampaign ( id int64 ) error {
// Delete all the campaign_results entries for this group
2014-03-26 04:53:51 +00:00
err := db . Debug ( ) . Delete ( & Result { CampaignId : id } ) . Error
/*_, err := Conn.Exec("DELETE FROM campaign_results WHERE cid=?", id)*/
2014-03-25 03:31:33 +00:00
if err != nil {
2014-03-26 04:53:51 +00:00
Logger . Println ( err )
2014-03-25 03:31:33 +00:00
return err
}
// Delete the reference to the campaign in the user_campaigns table
2014-03-26 04:53:51 +00:00
err = db . Debug ( ) . Delete ( & Campaign { Id : id } ) . Error
/*_, err = Conn.Exec("DELETE FROM user_campaigns WHERE cid=?", id)*/
2014-03-25 03:31:33 +00:00
if err != nil {
2014-03-26 04:53:51 +00:00
Logger . Panicln ( err )
2014-03-25 03:31:33 +00:00
return err
}
// Delete the campaign itself
2014-03-26 04:53:51 +00:00
/*_, err = Conn.Exec("DELETE FROM campaigns WHERE id=?", id)*/
2014-03-25 03:31:33 +00:00
return err
}