public IHttpActionResult PutValue(int id, ValueRequest valueRequest) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var value = db.Values.Find(id); if (User.Identity.GetUserId() != value.ApplicationUserId) { return BadRequest("This is not your value"); } Mapper.CreateMap<ValueRequest, Value>(); value = Mapper.Map<ValueRequest,Value>(valueRequest,value); db.Entry(value).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ValueExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public IHttpActionResult PostValue(ValueRequest valueRequest) { if (!ModelState.IsValid) { return BadRequest(ModelState); } Mapper.CreateMap<ValueRequest, Value>(); var value = Mapper.Map<Value>(valueRequest); // Set creation date value.CreationDate = DateTime.Now; value.ApplicationUserId = User.Identity.GetUserId(); db.Values.Add(value); db.SaveChanges(); Mapper.CreateMap<Value, ValueResponse>(); return CreatedAtRoute("DefaultApi", new { id = value.ValueId }, Mapper.Map<ValueResponse>(value)); }