public async Task<ActionResult> SubverseSettings(Subverse updatedModel) { try { if (ModelState.IsValid) { var existingSubverse = db.Subverses.Find(updatedModel.name); // check if subverse exists before attempting to edit it if (existingSubverse != null) { // check if user requesting edit is authorized to do so for current subverse // check that the user requesting to edit subverse settings is subverse owner! SubverseAdmin subAdmin = db.SubverseAdmins .Where(x => x.SubverseName == updatedModel.name && x.Username == User.Identity.Name && x.Power == 1).FirstOrDefault(); if (subAdmin != null) { // TODO investigate if EntityState is applicable here and use that instead // db.Entry(updatedModel).State = EntityState.Modified; existingSubverse.description = updatedModel.description; existingSubverse.sidebar = updatedModel.sidebar; existingSubverse.stylesheet = updatedModel.stylesheet; // these properties are currently not implemented but they can be saved and edited for future use existingSubverse.type = updatedModel.type; existingSubverse.label_submit_new_link = updatedModel.label_submit_new_link; existingSubverse.label_sumit_new_selfpost = updatedModel.label_sumit_new_selfpost; existingSubverse.submission_text = updatedModel.submission_text; existingSubverse.allow_default = updatedModel.allow_default; existingSubverse.rated_adult = updatedModel.rated_adult; existingSubverse.private_subverse = updatedModel.private_subverse; await db.SaveChangesAsync(); // go back to this subverse return RedirectToAction("Index", "Subverses", new { subversetoshow = updatedModel.name }); } else { // user was not authorized to commit the changes, drop attempt return new EmptyResult(); } } else { ModelState.AddModelError(string.Empty, "Sorry, The subverse you are trying to edit does not exist."); return View(); } } else { return View(); } } catch (Exception) { ModelState.AddModelError(string.Empty, "Something bad happened."); return View(); } }
public async Task<ActionResult> CreateSubverse([Bind(Include = "Name, Title, Description, Type, Sidebar, Creation_date, Owner")] AddSubverse subverseTmpModel) { try { if (ModelState.IsValid) { Subverse subverse = new Subverse(); subverse.name = subverseTmpModel.Name; subverse.title = "/v/" + subverseTmpModel.Name; subverse.description = subverseTmpModel.Description; subverse.type = subverseTmpModel.Type; subverse.sidebar = subverseTmpModel.Sidebar; subverse.creation_date = subverseTmpModel.Creation_date; //check if subverse exists before attempting to create it if (db.Subverses.Find(subverse.name) == null) { db.Subverses.Add(subverse); await db.SaveChangesAsync(); //register user as the owner of the newly created subverse SubverseAdmin tmpSubverseAdmin = new SubverseAdmin(); tmpSubverseAdmin.SubverseName = subverse.name; tmpSubverseAdmin.Username = subverseTmpModel.Owner; tmpSubverseAdmin.Power = 1; db.SubverseAdmins.Add(tmpSubverseAdmin); await db.SaveChangesAsync(); //subscribe user to the newly created subverse Whoaverse.Utils.User.SubscribeToSubverse(subverseTmpModel.Owner, subverse.name); //go to newly created Subverse return RedirectToAction("Index", "Subverses", new { subversetoshow = subverse.name }); } else { ModelState.AddModelError(string.Empty, "Sorry, The subverse you are trying to create already exists."); return View(); } } else { return View(); } } catch (Exception) { ModelState.AddModelError(string.Empty, "Something bad happened."); return View(); } }