public async Task <ResponseModel <EntryOutputModel> > Edit(int id, [FromBody] EntryInputModel entryInputModel) { try { if (ModelState.IsValid) { _logger.LogInformation($"Updating entry: {entryInputModel}"); var phonebook = await _entryService.Edit(id, entryInputModel); return(phonebook); } else { return(new ResponseModel <EntryOutputModel> { IsSuccessful = false, ResponseMessage = "Please make sure you have entered the correct details." }); } } catch (Exception exception) { _logger.LogError($"An exception occurred: {exception.ToString()}"); return(new ResponseModel <EntryOutputModel> { ResponseMessage = "An unexpected error occcurred", IsSuccessful = false }); } }
public async Task <ResponseModel <EntryOutputModel> > Edit(int entryId, EntryInputModel entryModel) { try { using (var ctx = new PhoneBookContext()) { var existinEntry = ctx.Entries.FirstOrDefault(x => x.EntryId == entryId); if (existinEntry.Name != existinEntry.Name || existinEntry.PhoneNumber != entryModel.PhoneNumber || existinEntry.PhoneBookId != entryModel.PhoneBookId) { existinEntry.Name = entryModel.Name; existinEntry.PhoneNumber = entryModel.PhoneNumber; existinEntry.PhoneBookId = entryModel.PhoneBookId; await ctx.SaveChangesAsync(); } return(new ResponseModel <EntryOutputModel> { DataSet = _mapper.Map <Core.Entry, EntryOutputModel>(existinEntry) }); } } catch (Exception ex) { return(new ResponseModel <EntryOutputModel> { ResponseMessage = Constants.UnexpectedError, IsSuccessful = false }); } }
public async Task <ResponseModel <EntryOutputModel> > Create(EntryInputModel entryModel) { try { using (var ctx = new PhoneBookContext()) { var entity = _mapper.Map <EntryInputModel, Core.Entry>(entryModel); ctx.Entries.Add(entity); await ctx.SaveChangesAsync(); return(new ResponseModel <EntryOutputModel> { DataSet = _mapper.Map <Core.Entry, EntryOutputModel>(entity) }); } } catch (Exception ex) { return(new ResponseModel <EntryOutputModel> { ResponseMessage = Constants.UnexpectedError, IsSuccessful = false }); } }
public EntryModel AddEntry(EntryInputModel entryInputModel) { return(Post <EntryModel, EntryInputModel>("mod_data_add_entry", entryInputModel)); }