Beispiel #1
0
        public CourseTeacherResponseDTO CreateTeacherOnCourse(CreateCourseTeacherRequestDTO request)
        {
            //provera da li postoji kurs
            CourseResponseDTO course = this._courseService.GetOneByUuid(request.courseUUID);

            //provera da li postoji profesor
            try
            {
                TeacherResponseDTO newTeacher = this._httpClientService.SendRequest <TeacherResponseDTO>(HttpMethod.Get, "http://localhost:40001/api/users/" + request.teacherUUID, new UserPrincipal(_httpContextAccessor.HttpContext).token).Result;
            }
            catch
            {
                throw new EntityNotFoundException($"Teacher with uuid: {request.teacherUUID} does not exist!", GeneralConsts.MICROSERVICE_NAME);
            }

            //provera da li vec postoji profesor na kursu
            CourseTeacher existingCourseTeacher = FindTeacherOnCourse(request.courseUUID, request.teacherUUID);

            if (existingCourseTeacher != null)
            {
                if (existingCourseTeacher.activeTeacher == false)
                {
                    CourseTeacher updatedTeacher = this._autoMapper.Map <CourseTeacher>(request);
                    updatedTeacher.activeTeacher = true;
                    updatedTeacher.course        = new Course()
                    {
                        uuid = request.courseUUID
                    };
                    updatedTeacher.teacher = new Teacher()
                    {
                        uuid = request.teacherUUID
                    };
                    updatedTeacher = this._queryExecutor.Execute <CourseTeacher>(DatabaseConsts.USER_SCHEMA, this._sqlCommands.UPDATE_TEACHER_COURSE(updatedTeacher), this._modelMapper.MapToCourseTeacher);
                    CourseTeacherResponseDTO updatedResponse = this._autoMapper.Map <CourseTeacherResponseDTO>(updatedTeacher);
                    return(connectWithUser(updatedResponse));
                }
                else
                {
                    throw new EntityAlreadyExistsException("Teacher with uuid " + request.courseUUID + " already exists in course with uuid " + request.teacherUUID, GeneralConsts.MICROSERVICE_NAME);
                }
            }
            CourseTeacher courseTeacher = this._autoMapper.Map <CourseTeacher>(request);

            courseTeacher.course = new Course()
            {
                uuid = request.courseUUID
            };
            courseTeacher.teacher = new Teacher()
            {
                uuid = request.teacherUUID
            };
            courseTeacher.activeTeacher = true;
            this._queryExecutor.Execute <CourseTeacher>(DatabaseConsts.USER_SCHEMA, this._sqlCommands.CREATE_TEACHER_COURSE(courseTeacher), this._modelMapper.MapToCourseTeacher);
            CourseTeacherResponseDTO response = this._autoMapper.Map <CourseTeacherResponseDTO>(courseTeacher);

            return(connectWithUser(response));
        }
        //DELETE METHODS
        public CourseResponseDTO Delete(string uuid)
        {
            //provera da li postoji kurs
            Course course = this.FindOneByUuidOrThrow(uuid);

            course = this._queryExecutor.Execute <Course>(DatabaseConsts.USER_SCHEMA, this._sqlCommands.DELETE_COURSE(uuid), this._modelMapper.MapToCourse);
            CourseResponseDTO response = this._autoMapper.Map <CourseResponseDTO>(course);

            response.subject = this._httpClientService.SendRequest <SubjectResponseDTO>(HttpMethod.Get, "http://localhost:40006/api/subjects/" + response.subject.uuid, new UserPrincipal(_httpContextAccessor.HttpContext).token).Result;
            return(response);
        }
        //PUT METHODS
        public CourseResponseDTO Update(UpdateCourseRequestDTO requestDTO)
        {
            //provera da li postoji kurs
            Course             oldCourse = this.FindOneByUuidOrThrow(requestDTO.uuid);
            SubjectResponseDTO subject;

            try
            {
                subject = this._httpClientService.SendRequest <SubjectResponseDTO>(HttpMethod.Get, "http://localhost:40006/api/subjects/" + requestDTO.subjectUUID, new UserPrincipal(_httpContextAccessor.HttpContext).token).Result;
            }
            catch
            {
                throw new EntityNotFoundException("Subject with uuid " + requestDTO.subjectUUID + " doesn't exist", GeneralConsts.MICROSERVICE_NAME);
            }

            //update u tabeli kurs
            Course course = new Course()
            {
                uuid         = requestDTO.uuid,
                name         = requestDTO.name,
                description  = requestDTO.description,
                active       = requestDTO.active,
                maxStudents  = requestDTO.maxStudents,
                minStudents  = requestDTO.minStudents,
                creationDate = requestDTO.creationDate,
                subject      = new Subject()
                {
                    uuid = requestDTO.subjectUUID
                }
            };

            this._queryExecutor.Execute <Course>(DatabaseConsts.USER_SCHEMA, this._sqlCommands.UPDATE_COURSE(course), this._modelMapper.MapToCourseAfterInsert);
            //insert u tabeli arhiva
            CreateCourseArchiveRequestDTO archive = new CreateCourseArchiveRequestDTO()
            {
                courseUUID    = course.uuid,
                name          = course.name,
                description   = course.description,
                active        = course.active,
                maxStudents   = course.maxStudents,
                minStudents   = course.minStudents,
                creationDate  = course.creationDate,
                subjectUUID   = subject.uuid,
                changeDate    = DateTime.Now,
                moderatorUUID = new UserPrincipal(_httpContextAccessor.HttpContext).uuid
            };

            _ = this._courseArchiveService.CreateCourseArchive(archive);
            CourseResponseDTO response = this._autoMapper.Map <CourseResponseDTO>(course);

            response.subject = subject;
            return(response);
        }
        public CourseResponseDTO GetOneByUuid(string uuid)
        {
            //provera da li postoji taj kurs
            CourseResponseDTO course = this._autoMapper.Map <CourseResponseDTO>(this.FindOneByUuidOrThrow(uuid));

            try {
                course.subject = this._httpClientService.SendRequest <SubjectResponseDTO>(HttpMethod.Get, "http://localhost:40006/api/subjects/" + course.subject.uuid, new UserPrincipal(_httpContextAccessor.HttpContext).token).Result;
            } catch {
                throw new EntityNotFoundException($"Subject with uuid: {course.subject.uuid} does not exist!", GeneralConsts.MICROSERVICE_NAME);
            }

            return(course);
        }
        //POST METHODS
        public CourseStudentResponseDTO CreateStudentOnCourse(CreateCourseStudentRequestDTO request)
        {
            //provera da li postoji kurs
            CourseResponseDTO  course    = this._courseService.GetOneByUuid(request.courseUUID);
            String             courseDep = course.subject.department.uuid;
            StudentResponseDTO student;

            //provera da li postoji student
            try {
                student = this._httpClientService.SendRequest <StudentResponseDTO>(HttpMethod.Get, "http://localhost:40001/api/users/students/" + request.studentUUID, new UserPrincipal(_httpContextAccessor.HttpContext).token).Result;
            }
            catch
            {
                throw new EntityNotFoundException("Student with uuid " + request.studentUUID + " doesn't exist", GeneralConsts.MICROSERVICE_NAME);
            }
            String studentDep = student.departmentUUID;

            //provera da li su predmet i student na istom departmanu
            if (studentDep != courseDep)
            {
                throw new EntityNotFoundException("Student with uuid " + request.studentUUID + " and course with uuid: " + request.courseUUID + " are not on the same department", GeneralConsts.MICROSERVICE_NAME);
            }
            //ako student vrsi zahtev, moze da upise samo sebe
            UserPrincipal up = new UserPrincipal(_httpContextAccessor.HttpContext);

            if (up.role == RoleConsts.ROLE_STUDENT_ONLY)
            {
                checkStudent(student.uuid, up.uuid);
            }
            //provera da li vec postoji student na tom kursu
            CourseStudent existingCourseStudent = this.FindStudentOnCourse(request.courseUUID, request.studentUUID);

            if (existingCourseStudent != null)
            {
                if (existingCourseStudent.activeStudent == false)
                {
                    CourseStudent updatedStudent = this._autoMapper.Map <CourseStudent>(request);
                    updatedStudent.activeStudent = true;
                    updatedStudent.course        = new Course()
                    {
                        uuid = request.courseUUID
                    };
                    updatedStudent.student = new Student()
                    {
                        uuid = request.studentUUID
                    };
                    updatedStudent = this._queryExecutor.Execute <CourseStudent>(DatabaseConsts.USER_SCHEMA, this._sqlCommands.UPDATE_STUDENT_ON_COURSE(updatedStudent), this._modelMapper.MapToCourseStudent);
                    CourseStudentResponseDTO updatedResponse = this._autoMapper.Map <CourseStudentResponseDTO>(updatedStudent);
                    return(connectWithUser(updatedResponse));
                }
                else
                {
                    throw new EntityAlreadyExistsException("Student with uuid " + request.studentUUID + " already exists in course with uuid " + request.courseUUID, GeneralConsts.MICROSERVICE_NAME);
                }
            }
            CourseStudent newCourseStudent = this._autoMapper.Map <CourseStudent>(request);

            newCourseStudent.activeStudent = true;
            newCourseStudent.course        = new Course()
            {
                uuid = request.courseUUID
            };
            newCourseStudent.student = new Student()
            {
                uuid = request.studentUUID
            };
            newCourseStudent = this._queryExecutor.Execute <CourseStudent>(DatabaseConsts.USER_SCHEMA, this._sqlCommands.CREATE_STUDENT_ON_COURSE(newCourseStudent), this._modelMapper.MapToCourseStudent);
            CourseStudentResponseDTO response = this._autoMapper.Map <CourseStudentResponseDTO>(newCourseStudent);

            response.course = this._courseService.GetOneByUuid(response.course.uuid);
            return(connectWithUser(response));
        }