Exemplo n.º 1
0
        public IActionResult Create(RoleCreateDTO req)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            var checkRole = _roleService.GetByCode(req.RoleCode);

            if (checkRole != null)
            {
                return(BadRequest(new
                {
                    errors = "Role Code Already Exist"
                }));
            }
            Role data = new Role
            {
                RoleCode     = req.RoleCode,
                RoleName     = req.RoleName,
                PermissionId = req.PermissionId
            };

            _roleService.Create(data);
            return(Ok());
        }
Exemplo n.º 2
0
        public RoleDTO CreateRole(RoleCreateDTO dto, string currentUserId)
        {
            var myEvent = _unitOfWork.Events.GetById(dto.EventId);

            if (myEvent == null || myEvent.User.Id != currentUserId)
            {
                throw new AppException("Event does not belong to you or does not exist", HttpStatusCode.Forbidden);
            }

            var role = _mapper.Map <Role>(dto);

            _unitOfWork.Roles.Add(role);
            _unitOfWork.Commit();

            return(new RoleDTO(role.Id, role.Name));
        }
        public async Task <IActionResult> CreateRole([FromBody] RoleCreateDTO role)
        {
            if ((await _context.Roles.FirstOrDefaultAsync(x => x.Name == role.Name))
                != null)
            {
                return(BadRequest(new
                {
                    error = "Already exists a role with this name"
                }));
            }

            var _role = _mapper.Map <Role>(role);
            await _context.Roles.AddAsync(_role);

            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(CreateRole), new { id = _role.Id }));
        }