public async Task <IHttpActionResult> AddEditPlan(DepartmentPlan plan) { string status = ""; if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //Edit or add depending on if id exists if (!PlanExists(plan.DepartmentPlanID)) { db.DepartmentPlans.Add(plan); await db.SaveChangesAsync(); status = "created"; } else { db.Entry(plan).State = EntityState.Modified; //When value is not specified for model DateTime property, the value defaults to 0001-01-01 //which is outside of the range of SQL Server's DATETIME plan.DateModified = DateTime.Now; try { await db.SaveChangesAsync(); status = "updated"; } catch (DbUpdateConcurrencyException) { if (!PlanExists(plan.DepartmentPlanID)) { return(NotFound()); } else { throw; } } } string message = $"Plan successfully { status }!"; return(Ok(new PostResponseViewModel { Ids = new List <int>() { plan.DepartmentPlanID }, Message = message })); }
public async Task <IHttpActionResult> Delete(int[] ids) { foreach (int i in ids) { DepartmentPlan plans = await db.DepartmentPlans.FindAsync(i); if (plans == null) { return(NotFound()); } db.DepartmentPlans.Remove(plans); } await db.SaveChangesAsync(); string message = "Plans deleted successfully!"; return(Ok(new PostResponseViewModel { Ids = null, Message = message })); }