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

            var validator           = new Dtos.AcademicDisciplineValidator();
            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.Teacher>().AnyAsync(d => d.Id == dto.AssistantId))
            {
                throw new InvalidModelException($"Teacher with id: {dto.AssistantId} not found");
            }

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

            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.Teacher>().AnyAsync(d => d.Id == dto.LecturerId))
            {
                throw new InvalidModelException($"Teacher with id: {dto.LecturerId} not found");
            }

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

            var discipline = await _context.FindAsync <Entities.AcademicDiscipline>(dto.Id);

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

            discipline.AssistantId          = dto.AssistantId;
            discipline.Attestation          = dto.Attestation;
            discipline.CurriculumId         = dto.CurriculumId;
            discipline.DepartmentId         = dto.DepartmentId;
            discipline.FullName             = dto.FullName;
            discipline.LecturerId           = dto.LecturerId;
            discipline.NumberOfCredits      = dto.NumberOfCredits;
            discipline.Semester             = dto.Semester;
            discipline.ShortName            = dto.ShortName;
            discipline.SpecialtyId          = dto.SpecialtyId;
            discipline.TypeOfIndividualWork = dto.TypeOfIndividualWork;
            discipline.UpdatedAt            = DateTime.UtcNow;

            await _context.SaveChangesAsync();
        }
示例#2
0
        /// <exception cref="InvalidModelException"/>
        public async Task <Guid> AddAsync(Dtos.AcademicDiscipline dto)
        {
            var validator           = new Dtos.AcademicDisciplineValidator();
            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 (dto.AssistantId != null && !await _context.Set <Entities.Teacher>().AnyAsync(d => d.Id == dto.AssistantId))
            {
                throw new InvalidModelException($"Teacher with id: {dto.AssistantId} not found");
            }

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

            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.Teacher>().AnyAsync(d => d.Id == dto.LecturerId))
            {
                throw new InvalidModelException($"Teacher with id: {dto.LecturerId} not found");
            }

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

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

            var academicDiscipline = new Entities.AcademicDiscipline
            {
                AssistantId          = dto.AssistantId,
                Attestation          = dto.Attestation,
                CreatedAt            = now,
                CurriculumId         = dto.CurriculumId,
                DepartmentId         = dto.DepartmentId,
                FullName             = dto.FullName,
                Id                   = id,
                LecturerId           = dto.LecturerId,
                Semester             = dto.Semester,
                NumberOfCredits      = dto.NumberOfCredits,
                ShortName            = dto.ShortName,
                SpecialtyId          = dto.SpecialtyId,
                TypeOfIndividualWork = dto.TypeOfIndividualWork,
                UpdatedAt            = now
            };

            await _context.AddAsync(academicDiscipline);

            await _context.SaveChangesAsync();

            return(id);
        }