Пример #1
0
        /// <exception cref="InvalidModelException"/>
        /// <exception cref="NotFoundException"/>
        public async Task UpdateAsync(Dtos.AcademicGroup dto)
        {
            if (dto.Id.Equals(Guid.Empty))
            {
                throw new InvalidModelException("Property Id failed validation. Error was: Id is empty");
            }

            var validator           = new Dtos.AcademicGroupValidator();
            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);
            }

            if (!await _context.Set <Entities.Department>().AnyAsync(d => d.Id == dto.DepartmentId))
            {
                throw new InvalidModelException($"Department with id: {dto.DepartmentId} not found");
            }

            if (!await _context.Set <Entities.EducationLevel>().AnyAsync(d => d.Id == dto.EducationLevelId))
            {
                throw new InvalidModelException($"Education level with id: {dto.EducationLevelId} not found");
            }

            if (!await _context.Set <Entities.FormOfEducation>().AnyAsync(d => d.Id == dto.FormOfEducationId))
            {
                throw new InvalidModelException($"Form of education with id: {dto.FormOfEducationId} not found");
            }

            if (!await _context.Set <Entities.StructuralUnit>().AnyAsync(d => d.Id == dto.StructuralUnitId))
            {
                throw new InvalidModelException($"Structural unit with id: {dto.StructuralUnitId} not found");
            }

            var specialty = await _context.FindAsync <Entities.Specialty>(dto.SpecialtyId);

            if (specialty == null)
            {
                throw new InvalidModelException($"Specialty with id: {dto.SpecialtyId} not found");
            }

            string code = $"{dto.Grade}{dto.Number:00}-{specialty.GroupsCode}";

            var group = await _context.FindAsync <Entities.AcademicGroup>(dto.Id);

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

            group.Captain           = dto.Captain;
            group.Code              = code;
            group.Curator           = dto.Curator;
            group.DepartmentId      = dto.DepartmentId;
            group.EducationLevelId  = dto.EducationLevelId;
            group.FormOfEducationId = dto.FormOfEducationId;
            group.Grade             = dto.Grade;
            group.Number            = dto.Number;
            group.SpecialtyId       = dto.SpecialtyId;
            group.StructuralUnitId  = dto.StructuralUnitId;
            group.UIN       = dto.UIN;
            group.UpdatedAt = DateTime.UtcNow;

            await _context.SaveChangesAsync();
        }
Пример #2
0
        /// <exception cref="InvalidModelException"/>
        /// <exception cref="NotFoundException"/>
        public async Task <Guid> AddAsync(Dtos.AcademicGroup dto)
        {
            var validator           = new Dtos.AcademicGroupValidator();
            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);
            }

            if (!await _context.Set <Entities.Department>().AnyAsync(d => d.Id == dto.DepartmentId))
            {
                throw new InvalidModelException($"Department with id: {dto.DepartmentId} not found");
            }

            if (!await _context.Set <Entities.EducationLevel>().AnyAsync(d => d.Id == dto.EducationLevelId))
            {
                throw new InvalidModelException($"Education level with id: {dto.EducationLevelId} not found");
            }

            if (!await _context.Set <Entities.FormOfEducation>().AnyAsync(d => d.Id == dto.FormOfEducationId))
            {
                throw new InvalidModelException($"Form of education with id: {dto.FormOfEducationId} not found");
            }

            if (!await _context.Set <Entities.StructuralUnit>().AnyAsync(d => d.Id == dto.StructuralUnitId))
            {
                throw new InvalidModelException($"Structural unit with id: {dto.StructuralUnitId} not found");
            }

            var specialty = await _context.FindAsync <Entities.Specialty>(dto.SpecialtyId);

            if (specialty == null)
            {
                throw new InvalidModelException($"Specialty with id: {dto.SpecialtyId} not found");
            }

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

            string code = $"{dto.Grade}{dto.Number:00}-{specialty.GroupsCode}";

            var academicGroup = new Entities.AcademicGroup
            {
                Captain           = dto.Captain,
                Code              = code,
                Curator           = dto.Curator,
                DepartmentId      = dto.DepartmentId,
                Id                = id,
                EducationLevelId  = dto.EducationLevelId,
                FormOfEducationId = dto.FormOfEducationId,
                Grade             = dto.Grade,
                Number            = dto.Number,
                StructuralUnitId  = dto.StructuralUnitId,
                SpecialtyId       = dto.SpecialtyId,
                UIN               = dto.UIN,
                CreatedAt         = now,
                UpdatedAt         = now
            };

            await _context.AddAsync(academicGroup);

            await _context.SaveChangesAsync();

            return(id);
        }