public Task <CommandExecutionResult <Student> > Handle(CreateStudentCommand request)
        {
            try
            {
                var student = new Student(request.FirstName, request.LastName, request.BirthDate, request.Gpi);

                _context.Set <Student>().Add(student);
                _context.SaveChangesAsync();


                return(Task.FromResult(new CommandExecutionResult <Student>()
                {
                    Success = true,
                    Data = student
                }));
            }
            catch (Exception exception)
            {
                return(Task.FromResult(new CommandExecutionResult <Student>()
                {
                    Success = false,
                    Exception = exception
                }));
            }
        }
Esempio n. 2
0
        public StudentDetailsDto Handle(StudentDetailsQuery request)
        {
            try
            {
                var student = _context.Set <StudentDetailsDto>().FirstOrDefault(std => std.StudentId == request.Id);

                if (student == null)
                {
                    return(default(StudentDetailsDto));
                }

                return(student);
            }
            catch (Exception exception)
            {
                return(default(StudentDetailsDto));
            }
        }
        public string Handle(UpdateStudentCommand request)
        {
            try
            {
                if (request.Id == 0)
                {
                    return("student should be more than 0");
                }

                var student = _context.Set <Student>().Find(request.Id);
                if (student == null)
                {
                    return("student not found");
                }

                student.FirstName = request.FirstName;
                student.LastName  = request.LastName;
                student.BirthDate = request.BirthDate;
                student.Gpi       = request.Gpi;

                var studentDetails = _context.Set <StudentDetailsDto>().Find(request.Id);
                if (studentDetails != null)
                {
                    studentDetails.FirstName = request.FirstName;
                    studentDetails.LastName  = request.LastName;
                    studentDetails.BirthDate = request.BirthDate;
                    studentDetails.GPI       = student.Gpi;
                }
                _context.SaveChanges();

                return("succeded");
            }
            catch (DbEntityValidationException exception)
            {
                return(exception.GetValidations());
            }
            catch (Exception exception)
            {
                return(exception.Message);
            }
        }
Esempio n. 4
0
        public Task <QueryExecutionResult <IEnumerable <Student> > > Handle(StudentListQuery request)
        {
            try
            {
                var students = _context.Set <Student>().AsNoTracking().ToList();

                return(Task.FromResult(new QueryExecutionResult <IEnumerable <Student> >()
                {
                    Success = true,
                    Data = students
                }));
            }
            catch (Exception exception)
            {
                return(Task.FromResult(new QueryExecutionResult <IEnumerable <Student> >()
                {
                    Success = false,
                    Exception = exception
                }));
            }
        }
        public IEnumerable <Course> Handle(CourseListQuery request)
        {
            try
            {
                var courses = _context.Set <Course>().AsEnumerable();

                if (courses == null)
                {
                    return(default(IEnumerable <Course>));
                }

                if (!string.IsNullOrWhiteSpace(request.Name))
                {
                    courses = courses.Where(course => course.Name.Contains(request.Name));
                }

                return(courses);
            }
            catch (Exception exception)
            {
                return(default(IEnumerable <Course>));
            }
        }
        public List <Course> GetCourseByStudentId(int Id)
        {
            var courses = _context.Set <Student>().Include("Courses").Where(x => x.Id == Id).SelectMany(student => student.Courses).ToList();

            return(courses == null ? default(List <Course>) : courses);
        }