public ActionResult Create(FormCollection collection, institution institution) { try { var newInstitution = db.institutions.Where(a => a.nameInstitution.Equals(institution.nameInstitution)).FirstOrDefault(); if (newInstitution == null) { db.institutions.Add(institution); db.SaveChanges(); Session.Add("message", "Institution added successfully"); Session["error"] = null; Session["notification"] = null; return(RedirectToAction("Index")); } else { Session.Add("error", "This institution already exists!"); Session["message"] = null; Session["notification"] = null; return(RedirectToAction("Create")); } } catch { return(View()); } }
public ActionResult Edit(int id, FormCollection collection, institution institution) { try { institution old = db.institutions.Single(i => i.idInstitution == id); if (db.institutions.Where(i => i.nameInstitution == institution.nameInstitution && i.idInstitution != institution.idInstitution).FirstOrDefault() == null) { UpdateModel(institution); db.SaveChanges(); Session.Add("message", "Institution updated successfully"); Session["error"] = null; Session["notification"] = null; return(RedirectToAction("Index")); } else { Session.Add("error", "An institution with the same name alreaddy exists!"); Session["message"] = null; Session["notification"] = null; return(RedirectToAction("Edit", id)); } } catch { return(View()); } }
public async Task <IActionResult> UpdateTodoItem(Guid id, institution institution) { if (id != institution.Id) { return(BadRequest()); } var todoItem = await _context.institutions.FindAsync(id); if (todoItem == null) { return(NotFound()); } todoItem.Name = institution.Name; todoItem.Code = institution.Code; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) when(!todoItemExists(id)) { return(NotFound()); } return(NoContent()); }
public override BaseEntity Add(object Obj) { institution institution = (institution)Obj; dbEntities = new mpi_dbEntities(); institution newinstitution = dbEntities.institutions.Add(institution); try { dbEntities.SaveChanges(); return(newinstitution); } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { Exception raise = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); // raise a new exception nesting // the current instance as InnerException raise = new InvalidOperationException(message, raise); } } throw raise; // return null; } }
public override BaseEntity GetById(object Id) { dbEntities = new mpi_dbEntities(); institution institution = (from c in dbEntities.institutions where c.id == (int)Id select c).SingleOrDefault(); return(institution); }
public override List <object> SqlList(string sql, int limit = 0, int offset = 0) { List <object> categoryList = new List <object>(); dbEntities = new mpi_dbEntities(); var institutions = dbEntities.institutions .SqlQuery(sql ). Select(institution => new { institution }); if (limit > 0) { institutions = institutions.Skip(offset * limit).Take(limit).ToList(); } else { institutions = institutions.ToList(); } foreach (var u in institutions) { institution institution = u.institution; categoryList.Add(institution); } count = countSQL(sql, dbEntities.institutions); return(categoryList); }
public override BaseEntity Update(object Obj) { dbEntities = new mpi_dbEntities(); institution institution = (institution)Obj; institution DBinstitution = (institution)GetById(institution.id); if (DBinstitution == null) { return(null); } dbEntities.Entry(DBinstitution).CurrentValues.SetValues(institution); dbEntities.SaveChanges(); return(institution); }
public ActionResult <institution> CreateInstitution(institution inst) { var institution = new institution { Code = inst.Code, Name = inst.Name }; _context.institutions.Add(institution); _context.SaveChangesAsync(); return(Created( nameof(GetInstitution), institution)); }
public override bool Delete(object Obj) { try { dbEntities = new mpi_dbEntities(); institution institution = (institution)Obj; dbEntities.institutions.Remove(institution); dbEntities.SaveChanges(); return(true); } catch (Exception e) { Console.WriteLine(e); return(false); } }
//********************* POST METHODS ********************* //must have an institution id to get it's children public void AddChild(institutionType NEWtype, string NEWcode, string NEWname, Guid parentId) { institution parentInst = _context.institutions.Find(parentId); if (parentInst == null) { throw new ArgumentNullException(nameof(parentInst)); } institution child = new institution { code = NEWcode, name = NEWname, type = NEWtype }; _context.institutions.Find(parentInst.id).children.Append(child); _context.institutions.Add(child); _context.SaveChanges(); /*var parent = _context.institutions.Find(parentId); * parent.children. * _context.institutions.Add(inst);*/ }
public void CreateInstitution(institution inst) { _context.institutions.Add(inst); _context.SaveChanges(); }
/*public ActivityCategory CreateActivityCategory(string NEWcode, string NEWname, Guid parentId) * { * institution tempinst = _context.institutions.Find(parentId); * if(tempinst == null) * { * throw new ArgumentNullException(nameof(tempinst)); * } * ActivityCategory NEWactivity = new ActivityCategory * { * code = NEWcode, * name = NEWname, * owner = parentId * }; * tempinst.activityCategories.Append(NEWactivity); * _context.SaveChanges(); * return NEWactivity; * } */ //********************* DELETE METHODS ********************* //must have an institution id to know what categorie to delete /*public void Delete(ActivityCategory act_cat) * { * institution tempinst = _context.institutions.Find(act_cat); * if (tempinst == null) * { * throw new ArgumentNullException(nameof(tempinst)); * } * tempinst.activityCategories.Remove(act_cat); * _context.SaveChanges(); * //_context.institutions.First(table => table.activityCategories).; * }*/ //********************* UPDATE METHODS ********************* public void UpdateInstitution(institution OLDinst) { _context.SaveChanges(); }