// GET /files/show/:id
func ShowFiles(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id, _ := strconv.ParseInt(vars["id"], 10, 64)
	file := models.FindFile(id)
	data := map[string]interface{}{"file": file}
	R.HTML(w, http.StatusOK, "files_show", data)
}
// POST /releases/changelog/{id}
func ChangelogReleases(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id, _ := strconv.ParseInt(vars["id"], 10, 64)
	release := models.FindRelease(id)
	file := models.FindFile(release.FileId)
	url := fmt.Sprintf("/changelog/%s.txt", file.Incremental)
	http.Redirect(w, r, url, http.StatusFound)
}
// POST /files/delete
func DeleteFiles(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	id, _ := strconv.ParseInt(r.FormValue("Id"), 10, 64)
	file := models.FindFile(id)

	// Delete from DB
	models.DeleteFile(file)

	http.Redirect(w, r, "/admin/files", http.StatusFound)
}
// POST /releases/delete
func DeleteReleases(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	id, _ := strconv.ParseInt(r.FormValue("Id"), 10, 64)
	release := models.FindRelease(id)
	file := models.FindFile(release.FileId)

	// Delete from DB
	models.DeleteRelease(release)
	models.UnpublishFile(file)
	go models.RefreshBuilds()

	http.Redirect(w, r, "/admin/releases", http.StatusFound)
}
// POST /releases/update
func UpdateReleases(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()

	// Parse form and append to struct
	id, _ := strconv.ParseInt(r.FormValue("Id"), 10, 64)
	fileId, _ := strconv.ParseInt(r.FormValue("FileId"), 10, 64)
	release := models.FindRelease(id)
	file := models.FindFile(fileId)
	release.FileId = file.Id
	release.FileName = file.Name
	release.Channel = r.FormValue("Channel")
	release.Changelog = r.FormValue("Changelog")

	// Append to db
	models.UpdateRelease(release)

	// Redirect
	http.Redirect(w, r, "/admin/releases", http.StatusFound)
}
// POST /releases/create
func CreateReleases(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	fileId, _ := strconv.ParseInt(r.FormValue("FileId"), 10, 64)
	file := models.FindFile(fileId)

	// Generate release
	release := models.Release{
		Created:   time.Now().UnixNano(),
		Changelog: r.FormValue("Changelog"),
		Channel:   r.FormValue("Channel"),
		FileId:    file.Id,
		FileName:  file.Name,
	}

	models.CreateRelease(release)
	models.PublishFile(file)
	go models.RefreshBuilds()

	http.Redirect(w, r, "/admin/releases", http.StatusFound)
}