public async Task <ActionResult <ContactLogModel> > Post(ContactLogModel model, string moniker) { try { var student = await _repository.GetStudentAsync(moniker); if (student == null) { return(BadRequest("Student not found.")); } var contact = _mapper.Map <ContactLog>(model); contact.Student = student; _repository.Add(contact); if (await _repository.SaveChangesAsync()) { var url = _linkGenerator.GetPathByAction(HttpContext, "Get", values: new { moniker, id = contact.Id }); return(Created(url, _mapper.Map <ContactLogModel>(contact))); } else { return(BadRequest("Failed to save new contact")); } } catch (Exception ex) { return(this.StatusCode(StatusCodes.Status500InternalServerError, ex)); } }
public async Task <ActionResult <ContactLogModel> > Put(string moniker, int id, ContactLogModel model) { try { var contact = await _repository.GetContactByMonikerAsync(moniker, id); if (contact == null) { return(NotFound("Could not find contact")); } _mapper.Map(model, contact); if (await _repository.SaveChangesAsync()) { return(_mapper.Map <ContactLogModel>(contact)); } else { return(BadRequest("Failed to update database.")); } } catch (Exception ex) { return(this.StatusCode(StatusCodes.Status500InternalServerError, ex)); } }