コード例 #1
0
        public async Task <ActionResult <ActorModel> > Post(ActorModel model)
        {
            try
            {
                var existing = _actorRepository.GetActorByNameAsync(model.LastName, model.FirstName);

                if (existing != null)
                {
                    return(Conflict(
                               $"The actor, {model.FirstName} {model.LastName}, already exists in the database."));
                }

                var actor = _mapper.Map <Actor>(model);

                actor.Id = _actorRepository.GenerateActorId();
                var location = _linkGenerator.GetPathByAction("Get", "Actors", new { actorId = actor.Id });

                _actorRepository.Add(actor);

                if (await _actorRepository.SaveChangesAsync())
                {
                    return(Created(location, _mapper.Map <ActorModel>(actor)));
                }

                return(Problem($"The actor, {model.FirstName} {model.LastName}, was not created.", "Add Actor",
                               StatusCodes.Status304NotModified));
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }