Esempio n. 1
0
 public ActionResult <RoleResponseDTO> Post([FromBody] RoleRequestDTO dto)
 {
     try
     {
         var role = _insertRole.Execute(dto);
         return(Created("api/roles/" + role.Id, role));
     }
     catch (EntityExistsException e)
     {
         return(Conflict(e.Message));
     }
     catch (Exception)
     {
         return(StatusCode(500, "Server error"));
     }
 }
Esempio n. 2
0
        public async Task <PageResult <RoleResponseDTO> > Query([FromBody] RoleRequestDTO input)
        {
            string likevalue = $"%{input.Name}%";
            var    query     = pcontext.Role.Where(u => EF.Functions.Like(u.Name, likevalue))
                               .Select(u => new RoleResponseDTO
            {
                Id         = u.Id,
                Name       = u.Name,
                CreateTime = u.CreateTime
            });
            var count = await query.CountAsync();

            var data = await query.Skip((input.PageIndex - 1) *input.PageSize).Take(input.PageSize)
                       .ToListAsync();

            return(new PageResult <RoleResponseDTO>(count, data));
        }
Esempio n. 3
0
 public IActionResult Put(int id, [FromBody] RoleRequestDTO roleDTO)
 {
     try
     {
         _updateRoleCommand.Execute(id, roleDTO);
         return(NoContent());
     }
     catch (EntityNotFoundException e)
     {
         return(NotFound(e.Message));
     }
     catch (EntityExistsException e)
     {
         return(Conflict(e.Message));
     }
     catch (Exception)
     {
         return(StatusCode(500, "Server error"));
     }
 }
        public RoleResponseDTO Execute(RoleRequestDTO request)
        {
            var role       = new Role();
            var roleExists = AiContext.Roles
                             .Where(x => x.Name == request.Name)
                             .Where(x => x.IsDeleted == 0)
                             .FirstOrDefault();

            if (roleExists != null)
            {
                throw new EntityExistsException("Role with that name");
            }
            role.Name = request.Name;
            AiContext.Roles.Add(role);
            AiContext.SaveChanges();
            return(new RoleResponseDTO
            {
                Id = role.Id,
                Name = role.Name
            });
        }
Esempio n. 5
0
        public void Execute(int id, RoleRequestDTO roleDTO)
        {
            var role = AiContext.Roles.Find(id);

            if (role == null || role.IsDeleted == 1)
            {
                throw new EntityNotFoundException("Role");
            }
            var roleExists = AiContext.Roles
                             .Where(x => x.Name == roleDTO.Name)
                             .Where(x => x.IsDeleted == 0)
                             .FirstOrDefault();

            if (roleExists != null)
            {
                throw new EntityExistsException("Role with that name");
            }
            role.Name       = roleDTO.Name;
            role.ModifiedAt = DateTime.UtcNow;
            AiContext.SaveChanges();
        }