public HttpResponseMessage CreateRelationship(HttpRequestMessage request, PersonRelationshipViewModel personRelationship)
        {
            return CreateHttpResponse(request, () =>
            {
                HttpResponseMessage response = null;

                if (!ModelState.IsValid)
                {
                    response = request.CreateResponse(HttpStatusCode.BadRequest,
                        ModelState.Keys.SelectMany(k => ModelState[k].Errors)
                              .Select(m => m.ErrorMessage).ToArray());
                }
                else
                {
                    PersonRelationship newPersonRelationship = new PersonRelationship();
                    newPersonRelationship.UpdatePersonRelationship(personRelationship);

                    _personRelationshipRepository.Add(newPersonRelationship);

                    _unitOfWork.Commit();

                    // Update view model
                    personRelationship = Mapper.Map<PersonRelationship, PersonRelationshipViewModel>(newPersonRelationship);
                    response = request.CreateResponse<PersonRelationshipViewModel>(HttpStatusCode.Created, personRelationship);
                }

                return response;
            });
        }
        public HttpResponseMessage Delete(HttpRequestMessage request, PersonRelationshipViewModel personRelationship)
        {
            return CreateHttpResponse(request, () =>
            {
                HttpResponseMessage response = null;

                if (!ModelState.IsValid)
                {
                    response = request.CreateResponse(HttpStatusCode.BadRequest,
                        ModelState.Keys.SelectMany(k => ModelState[k].Errors)
                              .Select(m => m.ErrorMessage).ToArray());
                }
                else
                {
                    var personRelationshipDb = _personRelationshipRepository.GetSingle(personRelationship.ID);
                    if (personRelationshipDb == null)
                        response = request.CreateErrorResponse(HttpStatusCode.NotFound, "Invalid.");
                    else
                    {
                        personRelationshipDb.UpdatePersonRelationship(personRelationship);
                        _personRelationshipRepository.Delete(personRelationshipDb);

                        _unitOfWork.Commit();
                        response = request.CreateResponse<PersonRelationshipViewModel>(HttpStatusCode.OK, personRelationship);
                    }
                }

                return response;
            });
        }
        public HttpResponseMessage Update(HttpRequestMessage request, PersonRelationshipViewModel personRelationship)
        {
            return CreateHttpResponse(request, () =>
            {
                HttpResponseMessage response = null;

                if (!ModelState.IsValid)
                {
                    response = request.CreateResponse(HttpStatusCode.BadRequest,
                        ModelState.Keys.SelectMany(k => ModelState[k].Errors)
                              .Select(m => m.ErrorMessage).ToArray());
                }
                else
                {
                    PersonRelationship _personRelationship = _personRelationshipRepository.GetSingle(personRelationship.ID);
                    _personRelationship.UpdatePersonRelationship(personRelationship);
                    _unitOfWork.Commit();

                    response = request.CreateResponse(HttpStatusCode.OK);
                }

                return response;
            });
        }
Exemplo n.º 4
0
 public static void UpdatePersonRelationship(this PersonRelationship personRelationship, PersonRelationshipViewModel personRelationshipVM)
 {
     personRelationship.RelationshipFromPersonId = personRelationshipVM.PersonId;
     personRelationship.RelationWithPersonId = personRelationshipVM.RelationWithPersonId;
     personRelationship.RelationshipTypeId = personRelationshipVM.RelationshipTypeId;
 }