public async Task <IActionResult> PutEngagementModel([FromRoute] int id, [FromBody] EngagementModel engagementModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != engagementModel.Id) { return(BadRequest()); } _context.Entry(engagementModel).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EngagementModelExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
/// <summary> /// Update chosen Engagement /// </summary> /// <param name="engagement"></param> /// <param name="id"></param> /// <returns></returns> public IHttpActionResult Put([FromBody] EngagementModel engagement, int id) { try { if (TimeKeeperUnit.Engagements.Get(id) == null) { Logger.Log($"No such engagement with id {id}"); return(NotFound()); } if (!ModelState.IsValid) { string message = "Failed updating engagement" + Environment.NewLine; message += string.Join(Environment.NewLine, ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)); throw new Exception(message); } TimeKeeperUnit.Engagements.Update(TimeKeeperFactory.Create(engagement, TimeKeeperUnit), id); TimeKeeperUnit.Save(); Logger.Log($"Updated engagement with id {id}", "INFO"); return(Ok(engagement)); } catch (Exception ex) { Logger.Log(ex.Message, "ERROR", ex); return(BadRequest(ex.Message)); } }
public async Task <IActionResult> PostEngagementModel([FromBody] EngagementModel engagementModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _context.EngagementModels.Add(engagementModel); await _context.SaveChangesAsync(); return(CreatedAtAction("GetEngagementModel", new { id = engagementModel.Id }, engagementModel)); }
/// <summary> /// Insert new Engagement /// </summary> /// <param name="engagement"></param> /// <returns></returns> public IHttpActionResult Post([FromBody] EngagementModel engagement) { try { if (!ModelState.IsValid) { string message = "Failed inserting new engagement" + Environment.NewLine; message += string.Join(Environment.NewLine, ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)); throw new Exception(message); } TimeKeeperUnit.Engagements.Insert(TimeKeeperFactory.Create(engagement, TimeKeeperUnit)); TimeKeeperUnit.Save(); Logger.Log("Inserted new engagement", "INFO"); return(Ok(engagement)); } catch (Exception ex) { Logger.Log(ex.Message, "ERROR", ex); return(BadRequest(ex.Message)); } }
public IHttpActionResult Post(EngagementModel e) { try { if (!ModelState.IsValid) { string message = "Failed inserting new member, " + Environment.NewLine; message += string.Join(Environment.NewLine, ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)); throw new Exception(message); } TimeKeeperUnit.Engagements.Insert(TimeKeeperFactory.Create(e, TimeKeeperUnit)); TimeKeeperUnit.Save(); Logger.Log($"Inserted new member {e.Employee.Name}", "INFO"); return(Ok(new { e, memberId = TimeKeeperUnit.Engagements.Get().Count() })); } catch (Exception ex) { Logger.Log(ex.Message, "ERROR", ex); return(BadRequest(ex.Message)); } }