Exemplo n.º 1
0
 public IActionResult Post([FromBody] RoleAddDTO request)
 {
     try
     {
         addCommand.Execute(request);
         return(StatusCode(201));
     }
     catch (EntityAlreadyExistsException e)
     {
         return(Conflict(new ErrorMessage {
             Message = e.Message
         }));
     }
 }
        public void Execute(RoleAddDTO request)
        {
            if (Context.Roles.Any(c => c.Name == request.Name))
            {
                throw new EntityAlreadyExistsException("Role", request.Name);
            }

            Role newRole = new Role
            {
                Name = request.Name.Trim()
            };

            Context.Roles.Add(newRole);

            Context.SaveChanges();
        }