Merge pull request #315 from rcutmore/group-editing

Add target update capability to API - @rcutmore
pull/322/head
Jordan Wright 2016-07-06 23:12:20 -05:00 committed by GitHub
commit d05a062a17
3 changed files with 75 additions and 10 deletions

View File

@ -128,8 +128,8 @@ func API_Campaigns_Id_Results(w http.ResponseWriter, r *http.Request) {
} }
} }
// API_Groups returns details about the requested group. If the campaign is not // API_Groups returns a list of groups if requested via GET.
// valid, API_Groups returns null. // If requested via POST, API_Groups creates a new group and returns a reference to it.
func API_Groups(w http.ResponseWriter, r *http.Request) { func API_Groups(w http.ResponseWriter, r *http.Request) {
switch { switch {
case r.Method == "GET": case r.Method == "GET":
@ -165,8 +165,8 @@ func API_Groups(w http.ResponseWriter, r *http.Request) {
} }
} }
// API_Groups_Id returns details about the requested campaign. If the group is not // API_Groups_Id returns details about the requested group.
// valid, API_Groups_Id returns null. // If the group is not valid, API_Groups_Id returns null.
func API_Groups_Id(w http.ResponseWriter, r *http.Request) { func API_Groups_Id(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
id, _ := strconv.ParseInt(vars["id"], 0, 64) id, _ := strconv.ParseInt(vars["id"], 0, 64)

View File

@ -123,14 +123,14 @@ func PutGroup(g *Group) error {
if err := g.Validate(); err != nil { if err := g.Validate(); err != nil {
return err return err
} }
// Fetch group's existing targets from database.
ts := []Target{} ts := []Target{}
ts, err = GetTargets(g.Id) ts, err = GetTargets(g.Id)
if err != nil { if err != nil {
Logger.Printf("Error getting targets from group ID: %d", g.Id) Logger.Printf("Error getting targets from group ID: %d", g.Id)
return err return err
} }
// Enumerate through, removing any entries that are no longer in the group // Check existing targets, removing any that are no longer in the group.
// For every target in the database
tExists := false tExists := false
for _, t := range ts { for _, t := range ts {
tExists = false tExists = false
@ -149,24 +149,25 @@ func PutGroup(g *Group) error {
} }
} }
} }
// Insert any entries that are not in the database // Add any targets that are not in the database yet.
// For every target in the new group
for _, nt := range g.Targets { for _, nt := range g.Targets {
// Check and see if the target already exists in the db // Check and see if the target already exists in the db
tExists = false tExists = false
for _, t := range ts { for _, t := range ts {
if t.Email == nt.Email { if t.Email == nt.Email {
tExists = true tExists = true
nt.Id = t.Id
break break
} }
} }
// If the target is not in the db, we add it // Add target if not in database, otherwise update target information.
if !tExists { if !tExists {
insertTargetIntoGroup(nt, g.Id) insertTargetIntoGroup(nt, g.Id)
} else {
UpdateTarget(nt)
} }
} }
err = db.Save(g).Error err = db.Save(g).Error
/*_, err = Conn.Update(g)*/
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
return err return err
@ -222,6 +223,20 @@ func insertTargetIntoGroup(t Target, gid int64) error {
return nil return nil
} }
// UpdateTarget updates the given target information in the database.
func UpdateTarget(target Target) error {
targetInfo := map[string]interface{}{
"first_name": target.FirstName,
"last_name": target.LastName,
"position": target.Position,
}
err := db.Model(&target).Where("id = ?", target.Id).Updates(targetInfo).Error
if err != nil {
Logger.Printf("Error updating target information for %s\n", target.Email)
}
return err
}
// GetTargets performs a many-to-many select to get all the Targets for a Group // GetTargets performs a many-to-many select to get all the Targets for a Group
func GetTargets(gid int64) ([]Target, error) { func GetTargets(gid int64) ([]Target, error) {
ts := []Target{} ts := []Target{}

View File

@ -63,6 +63,56 @@ func (s *ModelsSuite) TestPostGroupNoTargets(c *check.C) {
c.Assert(err, check.Equals, ErrNoTargetsSpecified) c.Assert(err, check.Equals, ErrNoTargetsSpecified)
} }
func (s *ModelsSuite) TestPutGroup(c *check.C) {
// Add test group.
group := Group{Name: "Put Test Group"}
group.Targets = []Target{
Target{Email: "test1@example.com", FirstName: "First", LastName: "Example"},
Target{Email: "test2@example.com", FirstName: "Second", LastName: "Example"},
}
group.UserId = 1
PostGroup(&group)
// Update one of group's targets.
group.Targets[0].FirstName = "New"
err := PutGroup(&group)
c.Assert(err, check.Equals, nil)
// Verify updated target information.
targets, _ := GetTargets(group.Id)
c.Assert(targets[0].Email, check.Equals, "test1@example.com")
c.Assert(targets[0].FirstName, check.Equals, "New")
c.Assert(targets[0].LastName, check.Equals, "Example")
c.Assert(targets[1].Email, check.Equals, "test2@example.com")
c.Assert(targets[1].FirstName, check.Equals, "Second")
c.Assert(targets[1].LastName, check.Equals, "Example")
}
func (s *ModelsSuite) TestPutGroupEmptyAttribute(c *check.C) {
// Add test group.
group := Group{Name: "Put Empty Attribute Test Group"}
group.Targets = []Target{
Target{Email: "test3@example.com", FirstName: "Third", LastName: "Example"},
Target{Email: "test4@example.com", FirstName: "Fourth", LastName: "Example"},
}
group.UserId = 1
PostGroup(&group)
// Update one of group's targets.
group.Targets[0].FirstName = ""
err := PutGroup(&group)
c.Assert(err, check.Equals, nil)
// Verify updated empty attribute was saved.
targets, _ := GetTargets(group.Id)
c.Assert(targets[0].Email, check.Equals, "test3@example.com")
c.Assert(targets[0].FirstName, check.Equals, "")
c.Assert(targets[0].LastName, check.Equals, "Example")
c.Assert(targets[1].Email, check.Equals, "test4@example.com")
c.Assert(targets[1].FirstName, check.Equals, "Fourth")
c.Assert(targets[1].LastName, check.Equals, "Example")
}
func (s *ModelsSuite) TestPostSMTP(c *check.C) { func (s *ModelsSuite) TestPostSMTP(c *check.C) {
smtp := SMTP{ smtp := SMTP{
Name: "Test SMTP", Name: "Test SMTP",