func AlbumMove(req *http.Request, r render.Render, su models.User, dbh *db.Dbh, p *models.Page) { // Default status var status string = "Permission Denied" var code int = 401 var reader io.Reader = req.Body b, e := ioutil.ReadAll(reader) if e != nil { log.Println(e) } var f Data err := json.Unmarshal(b, &f) if err != nil { log.Println(err) } log.Println(f) albumId := f.Md.AlbumId album := dbh.GetAlbumById(albumId) log.Println("album: ", albumId) if album.User == su.Id { status = "Success" code = 200 var image models.Image for _, f := range f.Md.Images { imageId, err := strconv.ParseInt(f.Id, 10, 10) if err != nil { log.Println("invalid image id: ", f.Id) } log.Println("Moving: ", f.Name, imageId) image = dbh.GetImageById(imageId) if image.User != su.Id { status = "Permission Denied" code = 401 continue } fromId := image.AlbumId image.AlbumId = albumId _ = dbh.UpdateImage(image) log.Printf("Moved %s from: %d to: %d status: %s code: %d\n", f.Name, fromId, albumId, status, code) } } p.Data = status encoder.Render("json", code, "", p, r) }
func AlbumUpdate(req *http.Request, r render.Render, su models.User, dbh *db.Dbh, p *models.Page) { p.SetUser(su) var reader io.Reader = req.Body b, e := ioutil.ReadAll(reader) if e != nil { log.Println(e) } var f interface{} err := json.Unmarshal(b, &f) if err != nil { log.Println(err) } m := f.(map[string]interface{}) md := m["data"].(map[string]interface{}) // Default status var status string = "Permission Denied" var code int = 401 mi := int64(md["Id"].(float64)) mo := int64(md["Owner"].(float64)) name := md["Name"].(string) album := dbh.GetAlbumById(mi) if album.Id == mi && album.User == mo && su.Id == mo { mAlbum := models.Album{ Id: mi, Description: md["Description"].(string), Name: md["Name"].(string), User: mo, Poster: md["Poster"].(string), Private: md["Private"].(bool), Privatekey: md["Privatekey"].(string)} log.Printf("mAlbum: %+v\n", mAlbum) uAlbum := dbh.AlbumUpdate(mAlbum) log.Printf("uAlbum: %+v\n", uAlbum) status = "Success" code = 200 } log.Println("Album Update: ", "Name: ", name, " Album Id: ", album.Id, "=", mi, " Album User: "******"=", mo, " Session: ", su.Id, "=", mo, " Status: ", status) p.Data = models.AlbumResult{Status: status} encoder.Render("json", code, "", p, r) }