Changed templates to have a 1-1 relationship with user (will implement sharing differently later)

Working on implementing /api/template/:id methods
Removed API_Campaigns_Id_Launch method (will just POST using template from angularjs scope later)
pull/24/head
Jordan 2014-05-28 18:48:30 -05:00
parent 6ec45ab927
commit 25cbaf92ce
4 changed files with 67 additions and 17 deletions

View File

@ -124,10 +124,6 @@ func API_Campaigns_Id(w http.ResponseWriter, r *http.Request) {
}
}
func API_Campaigns_Id_Launch(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", 302)
}
// API_Groups returns details about the requested group. If the campaign is not
// valid, API_Groups returns null.
// Example:
@ -267,7 +263,8 @@ func API_Templates(w http.ResponseWriter, r *http.Request) {
return
}
t.ModifiedDate = time.Now()
err = models.PostTemplate(&t, ctx.Get(r, "user_id").(int64))
t.UserId = ctx.Get(r, "user_id").(int64)
err = models.PostTemplate(&t)
if checkError(err, w, "Error inserting template", http.StatusInternalServerError) {
return
}
@ -280,7 +277,47 @@ func API_Templates(w http.ResponseWriter, r *http.Request) {
}
func API_Templates_Id(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", 302)
vars := mux.Vars(r)
id, _ := strconv.ParseInt(vars["id"], 0, 64)
switch {
case r.Method == "GET":
t, err := models.GetTemplate(id, ctx.Get(r, "user_id").(int64))
if checkError(err, w, "No template found", http.StatusNotFound) {
return
}
tj, err := json.MarshalIndent(t, "", " ")
if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) {
return
}
writeJSON(w, tj)
case r.Method == "DELETE":
err := models.DeleteTemplate(id, ctx.Get(r, "user_id").(int64))
if checkError(err, w, "Error deleting group", http.StatusInternalServerError) {
return
}
writeJSON(w, []byte("{\"success\" : \"true\"}"))
case r.Method == "PUT":
_, err := models.GetTemplate(id, ctx.Get(r, "user_id").(int64))
if checkError(err, w, "No group found", http.StatusNotFound) {
return
}
t := models.Template{}
err = json.NewDecoder(r.Body).Decode(&t)
if t.Id != id {
http.Error(w, "Error: /:id and template_id mismatch", http.StatusBadRequest)
return
}
t.ModifiedDate = time.Now()
err = models.PutTemplate(&t, ctx.Get(r, "user_id").(int64))
if checkError(err, w, "Error updating group", http.StatusInternalServerError) {
return
}
tj, err := json.MarshalIndent(t, "", " ")
if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) {
return
}
writeJSON(w, tj)
}
}
func writeJSON(w http.ResponseWriter, c []byte) {

View File

@ -50,7 +50,6 @@ func Setup() error {
db.CreateTable(Group{})
db.CreateTable(GroupTarget{})
db.CreateTable(Template{})
db.CreateTable(UserTemplate{})
db.CreateTable(Campaign{})
//Create the default user
init_user := User{

View File

@ -4,6 +4,7 @@ import "time"
type Template struct {
Id int64 `json:"id"`
UserId int64 `json:"-"`
Name string `json:"name"`
Text string `json:"text"`
Html string `json:"html"`
@ -18,32 +19,45 @@ type UserTemplate struct {
// GetTemplates returns the templates owned by the given user.
func GetTemplates(uid int64) ([]Template, error) {
ts := []Template{}
err := db.Table("templates t").Select("t.*").Joins("left join user_templates ut ON t.id = ut.template_id").Where("ut.user_id=?", uid).Scan(&ts).Error
err := db.Where("user_id=?", uid).Find(&ts).Error
if err != nil {
Logger.Println(err)
return ts, err
}
return ts, err
}
// GetTemplate returns the template, if it exists, specified by the given id and user_id.
func GetTemplate(id int64, uid int64) (Template, error) {
t := Template{}
err := db.Table("templates t").Select("t.*").Joins("left join user_templates ut ON t.id = ut.template_id").Where("ut.user_id=? and t.id=?", uid, id).Scan(&t).Error
err := db.Where("user_id=? and id=?", uid, id).Find(&t).Error
if err != nil {
Logger.Println(err)
return t, err
}
return t, err
}
// PostTemplate creates a new template in the database.
func PostTemplate(t *Template, uid int64) error {
func PostTemplate(t *Template) error {
// Insert into the DB
err := db.Save(t).Error
if err != nil {
Logger.Println(err)
return err
}
// Now, let's add the user->user_templates->template mapping
err = db.Exec("INSERT OR IGNORE INTO user_templates VALUES (?,?)", uid, t.Id).Error
if err != nil {
Logger.Printf("Error adding many-many mapping for template %s\n", t.Name)
}
return nil
}
func PutTemplate(t *Template, uid int64) error {
return nil
}
func DeleteTemplate(id int64, uid int64) error {
err := db.Debug().Where("user_id=?", uid).Delete(Template{Id: id}).Error
if err != nil {
Logger.Println(err)
return err
}
return nil
}

View File

@ -1,6 +1,6 @@
<!-- New Template Modal -->
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<button type="button" class="close" ng-click="cancel()">&times;</button>
<h4 class="modal-title" ng-hide="newTemplate" id="groupModalLabel">Edit Template: {{template.name}}</h4>
<h4 class="modal-title" ng-show="newTemplate" id="groupModalLabel">New Template</h4>
</div>
@ -34,6 +34,6 @@
</fieldset>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-default" ng-click="cancel()">Cancel</button>
<button type="button" class="btn btn-primary" ng-click="saveTemplate(template)" data-dismiss="modal">Save Template</button>
</div>