Beispiel #1
0
        /// <exception cref="InvalidModelException"/>
        /// <exception cref="NotFoundException"/>
        public async Task <Guid> AddAsync(Dtos.Privilege dto)
        {
            var validator           = new Dtos.PrivilegeValidator();
            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 id  = Guid.NewGuid();
            var now = DateTime.UtcNow;

            var privilege = new Entities.Privilege
            {
                Id        = id,
                CreatedAt = now,
                UpdatedAt = now,
                Name      = dto.Name,
            };

            await _context.AddAsync(privilege);

            await _context.SaveChangesAsync();

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

            var validator           = new Dtos.PrivilegeValidator();
            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 privilege = await _context.Set <Entities.Privilege>()
                            .FindAsync(dto.Id);

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

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

            await _context.SaveChangesAsync();
        }