Ejemplo n.º 1
0
        public async Task <ActionResult <IndividualModel> > Post(IndividualModel model)
        {
            var individual = _mapper.Map <Individual>(model);

            if (model.CityId != 0)
            {
                var city = await _repository.GetCityByIdAsync(model.CityId);

                if (city == null)
                {
                    return(BadRequest(_localizer["City with the given Id could not be found"]));
                }

                individual.City   = city;
                individual.CityId = city.Id;
            }

            _repository.Add(individual);

            if (await _repository.SaveChangesAsync())
            {
                var url = _linkGenerator.GetPathByAction("Get", "Individuals", new
                {
                    id = individual.Id
                });

                return(Created(url, _mapper.Map <IndividualModel>(individual)));
            }

            return(BadRequest());
        }
        public async Task <IActionResult> AddRelative(int id, int type, int relativeId)
        {
            if (await _repository.GetIndividualAsync(id) == null)
            {
                return(NotFound("Individual Could not be found"));
            }
            if (await _repository.GetIndividualAsync(relativeId) == null)
            {
                return(NotFound("Relative Could not be found"));
            }

            var relation = new Relation
            {
                Type         = (RelationType)type,
                IndividualId = id,
                RelativeId   = relativeId,
            };

            //if we want our database to reflect also the reverse relationship, we should construct another relation object
            //depending on business requirements
            _repository.Add(relation);

            if (await _repository.SaveChangesAsync())
            {
                return(Ok(relation));
            }

            return(BadRequest());
        }