public async Task <IActionResult> Put(PersonRequestModel personRequestModel) { var isSaved = await _personData.UpdatePerson(personRequestModel); if (isSaved) { return(Ok(isSaved)); } return(BadRequest("bad request")); }
public async Task <IActionResult> Post(PersonRequestModel personRequestModel) { var isSaved = await _personData.SavePerson(personRequestModel); if (isSaved) { return(Ok(isSaved)); } return(BadRequest("Person is not mathed with Rule.")); }
public async Task <ActionResult <PersonModel> > PostAsync(PersonRequestModel personRequestModel) { var response = await _personService.PostAsync(personRequestModel); if (response.IsError) { return(StatusCode((int)response.ErrorCode, response.ErrorDescription)); } return(Ok(response.Data)); }
public async Task <bool> UpdatePerson(PersonRequestModel personRequestModel) { var person = await _ruleDbContext.Persons.FirstOrDefaultAsync(x => x.Id == personRequestModel.Id); if (person == null) { return(false); } _ruleDbContext.Persons.Update(_mapper.Map(personRequestModel, person)); return(await _ruleDbContext.SaveChangesAsync() > 0); }
public HttpResponseMessage PutPerson(int id, PersonRequestModel requestModel) { // Change tracker has a deep knowladge on the Timestamp property and // it sends the first retrieved Timestamp no matter what the latter value is. // That's why we are doing the operations with two seperate context objects. // ref: http://stackoverflow.com/questions/4402586/optimisticconcurrencyexception-does-not-work-in-entity-framework-in-certain-situ // http://stackoverflow.com/questions/13581473/why-does-the-objectstatemanager-property-not-exist-in-my-db-context using (ConfContext _confContext = new ConfContext()) { Person existingPerson = _confContext.People.FirstOrDefault(personEntity => personEntity.Id == id); if (existingPerson == null) { return(new HttpResponseMessage(HttpStatusCode.NotFound)); } string ifMatchHeader; if (Request.Headers.IfMatch.TryGetETag(out ifMatchHeader)) { //ObjectStateManager objectStateManager = ((IObjectContextAdapter)_confContext).ObjectContext.ObjectStateManager; //objectStateManager.ChangeObjectState(existingPerson, EntityState.Detached); _confContext.Entry(existingPerson).State = EntityState.Detached; ifMatchHeader = ifMatchHeader.Trim('"'); byte[] timestamp = Convert.FromBase64String(ifMatchHeader); existingPerson.Timestamp = timestamp; Person person = Mapper.Map <PersonRequestModel, Person>(requestModel, existingPerson); try { _confContext.Entry(person).State = EntityState.Modified; _confContext.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { const string message = "The record you attempted to edit was modified by another user after you got the original value."; return(Request.CreateErrorResponse(HttpStatusCode.PreconditionFailed, message)); } catch (DataException) { //Log the error (add a variable name after Exception) const string message = "Unable to save changes. Try again, and if the problem persists contact your system administrator."; return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, message)); } return(Request.CreateResponse(HttpStatusCode.OK, Mapper.Map <Person, PersonDto>(person))); } return(Request.CreateErrorResponse(HttpStatusCode.PreconditionFailed, "If-Match header needs to be provided in order to update the entity.")); } }
public HttpResponseMessage PostPerson(PersonRequestModel requestModel) { using (ConfContext _confContext = new ConfContext()) { Person person = Mapper.Map <PersonRequestModel, Person>(requestModel); _confContext.People.Add(person); int result = _confContext.SaveChanges(); if (result > 0) { return(Request.CreateResponse(HttpStatusCode.Created, Mapper.Map <Person, PersonDto>(person))); } return(Request.CreateResponse(HttpStatusCode.Conflict)); } }
public async Task <ServiceResponse <PersonModel> > PostAsync(PersonRequestModel personRequestModel) { try { var person = await _context.People.AsNoTracking().FirstOrDefaultAsync(f => f.Name == personRequestModel.Name && f.DateOfBirth == personRequestModel.DateOfBirth); if (person != null) { return(new ServiceResponse <PersonModel> { ErrorCode = HttpStatusCode.Conflict, ErrorDescription = "Person already exists" }); } var newPerson = _mapper.Map <Person>(personRequestModel); _context.Entry(newPerson).State = EntityState.Added; if (await _context.SaveChangesAsync() > 0) { return(new ServiceResponse <PersonModel> { Data = _mapper.Map <PersonModel>(newPerson) }); } return(new ServiceResponse <PersonModel> { ErrorCode = HttpStatusCode.InternalServerError, ErrorDescription = "Internal Server Error" }); } catch (Exception e) { Debug.WriteLine(e); return(new ServiceResponse <PersonModel> { ErrorCode = HttpStatusCode.InternalServerError, ErrorDescription = "Internal Server Error" }); } }
private async Task <bool> AddPerson(PersonRequestModel personRequestModel) { _ruleDbContext.Persons.Add(_mapper.Map <Model.Person>(personRequestModel)); return(await _ruleDbContext.SaveChangesAsync() > 0); }
public async Task <bool> SavePerson(PersonRequestModel personRequestModel) { return(await AddPerson(personRequestModel)); }