Ejemplo n.º 1
0
        public async Task <IActionResult> PostLocationClassroom([FromRoute] int id, [FromBody] Classrooms classroom)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.Classrooms.Add(classroom);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetLocation", new { id = classroom.LocationId }, classroom));
        }
        public async Task <Unit> Handle(UpdateLectorSubjectCommand request, CancellationToken cancellationToken)
        {
            var lectorSubject = await _context.LectorSubjects.FindAsync(request.Id);

            if (lectorSubject == null)
            {
                throw new NotFoundException(nameof(LectorSubject), request.Id);
            }

            var isLectorSubjectExist = await _context.LectorSubjects.AnyAsync(x => x.Id != request.Id &&
                                                                              x.LectorId == request.LectorId &&
                                                                              x.SubjectId == request.SubjectId &&
                                                                              x.LessonTypeId == request.LessonTypeId);

            if (isLectorSubjectExist)
            {
                throw new DuplicateException(nameof(LectorSubject));
            }

            lectorSubject.LectorId     = request.LectorId;
            lectorSubject.SubjectId    = request.SubjectId;
            lectorSubject.LessonTypeId = request.LessonTypeId;

            _context.LectorSubjects.Update(lectorSubject);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Ejemplo n.º 3
0
        public async Task <Unit> Handle(CreateAttendanceCommand request, CancellationToken cancellationToken)
        {
            var lesson = await _context.Lessons
                         .FirstOrDefaultAsync(x => x.LectorSubjectId == request.LectorSubjectId && x.GroupId == request.GroupId);

            if (lesson == null)
            {
                throw new NotFoundException(nameof(Lesson));
            }

            var attendance = new Attendance
            {
                DayOfWeekId   = request.DayOfWeekId,
                StudentsCount = request.StudentsCount,
                Date          = request.Date,
                LessonId      = lesson.Id,
                WeatherTypeId = request.WeatherTypeId
            };

            _context.Attendances.Add(attendance);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Ejemplo n.º 4
0
        public async Task <Unit> Handle(UpdateLectorCommand request, CancellationToken cancellationToken)
        {
            var lector = await _context.Lectors.FindAsync(request.Id);

            if (lector == null)
            {
                throw new NotFoundException(nameof(Lector), request.Id);
            }

            var isEmailExist = await _context.Lectors.AnyAsync(x => x.Email == request.Email && x.Id != lector.Id);

            if (isEmailExist)
            {
                throw new DuplicateException(nameof(Lector), "Email", request.Email);
            }

            lector.FirstName        = request.FirstName;
            lector.LastName         = request.LastName;
            lector.Patronymic       = request.Patronymic;
            lector.PhoneNumber      = request.PhoneNumber;
            lector.Email            = request.Email;
            lector.AcademicDegreeId = request.AcademicDegreeId;
            lector.AcademicRankId   = request.AcademicRankId;

            var user = await _context.Users.SingleOrDefaultAsync(x => x.Id == lector.UserId);

            user.Email = request.Email;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Ejemplo n.º 5
0
        public async Task <int> Handle(CreateLectorSubjectCommand request, CancellationToken cancellationToken)
        {
            var all = await _context.LectorSubjects.ToListAsync();

            var isLectorSubjectExist = await _context.LectorSubjects.AnyAsync(x => x.LectorId == request.LectorId &&
                                                                              x.SubjectId == request.SubjectId &&
                                                                              x.LessonTypeId == request.LessonTypeId);

            if (isLectorSubjectExist)
            {
                throw new DuplicateException(nameof(LectorSubject));
            }

            var lectorSubject = new LectorSubject
            {
                LectorId     = request.LectorId,
                SubjectId    = request.SubjectId,
                LessonTypeId = request.LessonTypeId
            };

            _context.LectorSubjects.Add(lectorSubject);

            await _context.SaveChangesAsync(cancellationToken);

            return(lectorSubject.Id);
        }
Ejemplo n.º 6
0
        public async Task <int> Handle(UpdateGroupCommand request, CancellationToken cancellationToken)
        {
            var group = await _context.Groups.FindAsync(request.Id);

            if (group == null)
            {
                throw new NotFoundException(nameof(Group), request.Id);
            }

            var isGroupNameExist = await _context.Groups.AnyAsync(x => x.Name == request.Name && x.Id != group.Id);

            if (isGroupNameExist)
            {
                throw new DuplicateException(nameof(Group), "Name", request.Name);
            }

            group.Name                = request.Name;
            group.SpecialityId        = request.SpecialityId;
            group.StudentsCount       = request.StudentsCount;
            group.Year                = request.Year;
            group.EducationalDegreeId = request.EducationalDegreeId;

            _context.Groups.Update(group);

            await _context.SaveChangesAsync(cancellationToken);

            return(group.Id);
        }
Ejemplo n.º 7
0
        public async Task <Unit> Handle(DeleteLectorCommand request, CancellationToken cancellationToken)
        {
            var lector = await _context.Lectors.FindAsync(request.Id);

            if (lector == null)
            {
                throw new NotFoundException(nameof(Lector), request.Id);
            }

            var isUsedInLectorSubject = await _context.LectorSubjects.AnyAsync(x => x.LectorId == request.Id);

            if (isUsedInLectorSubject)
            {
                throw new DeleteFailureException(nameof(Lector), request.Id, "There are subject(s) assigned to this lector");
            }

            var user = await _context.Users.FindAsync(lector.UserId);

            var userRoles = await _context.UserRoles.Where(x => x.UserId == user.Id).ToListAsync();

            _context.UserRoles.RemoveRange(userRoles);
            _context.Users.Remove(user);

            _context.Lectors.Remove(lector);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Ejemplo n.º 8
0
        public async Task <int> Handle(CreateSubjectCommand request, CancellationToken cancellationToken)
        {
            var subject = new Subject
            {
                Name = request.Name
            };

            _context.Subjects.Add(subject);

            await _context.SaveChangesAsync(cancellationToken);

            return(subject.Id);
        }
Ejemplo n.º 9
0
        public async Task <Unit> Handle(DeleteLessonCommand request, CancellationToken cancellationToken)
        {
            var lesson = await _context.Lessons.FindAsync(request.Id);

            if (lesson == null)
            {
                throw new NotFoundException(nameof(Lesson), request.Id);
            }

            _context.Lessons.Remove(lesson);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(DeleteAttendanceCommand request, CancellationToken cancellationToken)
        {
            var attendance = _context.Attendances.FindAsync(request.Id);

            if (attendance == null)
            {
                throw new NotFoundException(nameof(Attendance), request.Id);
            }

            _context.Remove(attendance);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Ejemplo n.º 11
0
        public async Task <Unit> Handle(UpdateSubjectCommand request, CancellationToken cancellationToken)
        {
            var subject = await _context.Subjects.FindAsync(request.Id);

            if (subject == null)
            {
                throw new NotFoundException(nameof(Subject), request.Id);
            }

            subject.Name = request.Name;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Ejemplo n.º 12
0
        public async Task <Unit> Handle(UpdateLessonCommand request, CancellationToken cancellationToken)
        {
            var lesson = await _context.Lessons.FindAsync(request.Id);

            if (lesson == null)
            {
                throw new NotFoundException(nameof(Lesson), request.Id);
            }

            lesson.LectorSubjectId = request.LectorSubjectId;
            lesson.GroupId         = request.GroupId;

            _context.Lessons.Update(lesson);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Ejemplo n.º 13
0
        public static void LoadSubjectsTestData(CampusDbContext context)
        {
            context.AcademicRanks.Add(new AcademicRank {
                Id = 1, Name = "Academic rank 1"
            });
            context.AcademicDegrees.Add(new AcademicDegree {
                Id = 1, Name = "Academic degree 1"
            });
            context.Users.Add(new User {
                Id = 100, Email = "*****@*****.**", PasswordHash = "testPassword123", LastVisit = DateTime.Now
            });
            context.UserRoles.Add(new UserRole {
                RoleId = 1, UserId = 100
            });
            context.Lectors.Add(new Lector
            {
                Id               = 1,
                FirstName        = "Name 1",
                LastName         = "LastName 1",
                Patronymic       = "Patronymic 1",
                AcademicDegreeId = 1,
                AcademicRankId   = 1,
                UserId           = 100,
                Email            = "*****@*****.**",
                PhoneNumber      = "+99999999999"
            });
            context.LessonTypes.Add(new LessonType {
                Id = 1, Name = "Lesson type 1"
            });

            context.Subjects.Add(new Subject {
                Id = 1, Name = "Subject 1"
            });
            context.Subjects.Add(new Subject {
                Id = 2, Name = "Subject 2"
            });

            context.LectorSubjects.Add(new LectorSubject {
                LectorId = 1, SubjectId = 2
            });

            context.SaveChangesAsync();
        }
        public async Task <int> Handle(CreateSpecialityCommand request, CancellationToken cancellationToken)
        {
            var isSpecialityCodeExist = await _context.Specialities.AnyAsync(x => x.Code == request.Code);

            if (isSpecialityCodeExist)
            {
                throw new DuplicateException(nameof(Speciality), "Code", request.Code);
            }

            var speciality = new Speciality
            {
                Name = request.Name,
                Code = request.Code
            };

            _context.Specialities.Add(speciality);

            await _context.SaveChangesAsync(cancellationToken);

            return(speciality.Id);
        }
Ejemplo n.º 15
0
        public async Task <Unit> Handle(DeleteLectorSubjectCommand request, CancellationToken cancellationToken)
        {
            var lectorSubject = await _context.LectorSubjects.FindAsync(request.Id);

            if (lectorSubject == null)
            {
                throw new NotFoundException(nameof(LectorSubject), request.Id);
            }

            var isUsedInLessons = await _context.Lessons.AnyAsync(x => x.LectorSubjectId == lectorSubject.Id);

            if (isUsedInLessons)
            {
                throw new DeleteFailureException(nameof(LectorSubject), request.Id, "This record is used in lesson(s)");
            }

            _context.LectorSubjects.Remove(lectorSubject);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Ejemplo n.º 16
0
        public async Task <Unit> Handle(DeleteSubjectCommand request, CancellationToken cancellationToken)
        {
            var subject = await _context.Subjects.FindAsync(request.Id);

            if (subject == null)
            {
                throw new NotFoundException(nameof(Subject), request.Id);
            }

            var usedInLectorSubject = await _context.LectorSubjects.AnyAsync(x => x.SubjectId == subject.Id);

            if (usedInLectorSubject)
            {
                throw new DeleteFailureException(nameof(Subject), request.Id, "This subjects is assigned to lectors");
            }

            _context.Subjects.Remove(subject);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Ejemplo n.º 17
0
        public async Task <Unit> Handle(DeleteGroupCommand request, CancellationToken cancellationToken)
        {
            var group = await _context.Groups.FindAsync(request.Id);

            if (group == null)
            {
                throw new NotFoundException(nameof(Group), request.Id);
            }

            var isUsedInLessons = await _context.Lessons.AnyAsync(x => x.GroupId == group.Id);

            if (isUsedInLessons)
            {
                throw new DeleteFailureException(nameof(Group), request.Id, "There are lesson(s) with this group");
            }

            _context.Groups.Remove(group);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Ejemplo n.º 18
0
        public async Task <Unit> Handle(DeleteSpecialityCommand request, CancellationToken cancellationToken)
        {
            var speciality = await _context.Specialities.FindAsync(request.Id);

            if (speciality == null)
            {
                throw new NotFoundException(nameof(Speciality), request.Id);
            }

            var groupsWithSpecialityExist = await _context.Groups.AnyAsync(x => x.SpecialityId == request.Id);

            if (groupsWithSpecialityExist)
            {
                throw new DeleteFailureException(nameof(Speciality), request.Id, "There are group(s) on this speciality");
            }

            _context.Specialities.Remove(speciality);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(UpdateSpecialityCommand request, CancellationToken cancellationToken)
        {
            var speciality = await _context.Specialities.FindAsync(request.Id);

            if (speciality == null)
            {
                throw new NotFoundException(nameof(Speciality), request.Id);
            }

            var isSpecialityCodeExist = await _context.Specialities.AnyAsync(x => x.Code == request.Code && x.Id != speciality.Id);

            if (isSpecialityCodeExist)
            {
                throw new DuplicateException(nameof(Speciality), "Code", request.Code);
            }

            speciality.Name = request.Name;
            speciality.Code = request.Code;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Ejemplo n.º 20
0
        public async Task <int> Handle(CreateGroupCommand request, CancellationToken cancellationToken)
        {
            var isGroupNameExist = await _context.Groups.AnyAsync(x => x.Name == request.Name);

            if (isGroupNameExist)
            {
                throw new DuplicateException(nameof(Group), "Name", request.Name);
            }

            var group = new Group
            {
                Name                = request.Name,
                SpecialityId        = request.SpecialityId,
                EducationalDegreeId = request.EducationalDegreeId,
                StudentsCount       = request.StudentsCount,
                Year                = request.Year
            };

            _context.Groups.Add(group);

            await _context.SaveChangesAsync(cancellationToken);

            return(group.Id);
        }
Ejemplo n.º 21
0
        public async Task <Unit> Handle(CreateLessonCommand request, CancellationToken cancellationToken)
        {
            var lectorSubject = await _context.LectorSubjects.Where(x => x.SubjectId == request.SubjectId &&
                                                                    x.LessonTypeId == request.LessonTypeId &&
                                                                    x.LectorId == request.LectorId)
                                .FirstOrDefaultAsync();

            if (lectorSubject == null)
            {
                throw new NotFoundException(nameof(LectorSubject));
            }

            var lesson = new Lesson
            {
                LectorSubjectId = lectorSubject.Id,
                GroupId         = request.GroupId
            };

            _context.Lessons.Add(lesson);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }