Example #1
0
        /// <exception cref="InvalidModelException"/>
        public async Task <Guid> AddAsync(RoleDto role)
        {
            var validator           = new Dtos.RoleValidator();
            ValidationResult result = validator.Validate(role);

            if (!result.IsValid)
            {
                string errMess = string.Empty;

                foreach (var failure in result.Errors)
                {
                    errMess += $"Property { failure.PropertyName } failed validation. Error was: { failure.ErrorMessage }\n";
                }

                throw new InvalidModelException(errMess);
            }

            var id  = Guid.NewGuid();
            var now = DateTime.UtcNow;

            var roleEntity = new Entities.Role
            {
                Id        = id,
                CreatedAt = now,
                UpdatedAt = now,
                Name      = role.Name
            };

            await _context.AddAsync(roleEntity);

            await _context.SaveChangesAsync();

            return(id);
        }
Example #2
0
        /// <exception cref="InvalidModelException"/>
        /// <exception cref="NotFoundException"/>
        public async Task UpdateAsync(RoleDto dto)
        {
            if (dto.Id.Equals(Guid.Empty))
            {
                throw new InvalidModelException("Property Id failed validation. Error was: Id is empty");
            }

            var validator           = new Dtos.RoleValidator();
            ValidationResult result = validator.Validate(dto);

            if (!result.IsValid)
            {
                string errMess = string.Empty;

                foreach (var failure in result.Errors)
                {
                    errMess += $"Property { failure.PropertyName } failed validation. Error was: { failure.ErrorMessage }\n";
                }

                throw new InvalidModelException(errMess);
            }

            var role = await _context.FindAsync <Entities.Role>(dto.Id);

            if (role == null)
            {
                throw new NotFoundException($"Entity with id: {dto.Id} not found.");
            }

            role.UpdatedAt = DateTime.UtcNow;
            role.Name      = dto.Name;

            await _context.SaveChangesAsync();
        }