Ejemplo n.º 1
0
            public async Task <Unit> Handle(Execute request, CancellationToken cancellationToken)
            {
                Guid _courseId = Guid.NewGuid();

                if (request.CourseId != null)
                {
                    _courseId = request.CourseId ?? Guid.NewGuid();
                }

                var course = new Course
                {
                    CourseId          = _courseId,
                    Title             = request.Title,
                    Description       = request.Description,
                    DateOfPublication = request.DateOfPublication,
                    CreationDate      = DateTime.UtcNow
                };

                await _context.Course.AddAsync(course);


                if (request.ListInstructor != null)
                {
                    foreach (var id in request.ListInstructor)
                    {
                        var cursoInstructor = new CourseInstructor {
                            CourseId     = _courseId,
                            InstructorId = id
                        };
                        await _context.CourseInstructor.AddAsync(cursoInstructor);
                    }
                }

                /*Add logic insert price course*/
                var priceEntity = new Price {
                    CourseId    = _courseId,
                    ActualPrice = request.Price,
                    Promotion   = request.Promotion,
                    PriceId     = Guid.NewGuid()
                };

                await _context.Price.AddAsync(priceEntity);


                var value = await _context.SaveChangesAsync();

                if (value > 0)
                {
                    return(Unit.Value);
                }

                throw new Exception("Someting wrong, dont insert course");
            }
Ejemplo n.º 2
0
            public async Task <Unit> Handle(Execute request, CancellationToken cancellationToken)
            {
                var comment = await _context.Comment.FindAsync(request.Id);

                if (comment == null)
                {
                    throw new HandlerException(HttpStatusCode.NotFound, new { message = "Dont found comment" });
                }

                _context.Remove(comment);
                var result = await _context.SaveChangesAsync();

                if (result > 0)
                {
                    return(Unit.Value);
                }

                throw new Exception("Something wrong, dont delete comment");
            }
Ejemplo n.º 3
0
            public async Task <Unit> Handle(Execute request, CancellationToken cancellationToken)
            {
                //back list reference instructor of course
                var instructorSDB = _context.CourseInstructor.Where(x => x.CourseId == request.Id);

                foreach (var instructor in instructorSDB)
                {
                    _context.CourseInstructor.Remove(instructor);
                }

                /*Delete comments for DB*/
                var commentsDB = _context.Comment.Where(x => x.CourseId == request.Id);

                foreach (var comment in commentsDB)
                {
                    _context.Comment.Remove(comment);
                }

                /*Delete price */
                var precioDB = _context.Price.Where(x => x.CourseId == request.Id).FirstOrDefault();

                if (precioDB != null)
                {
                    _context.Price.Remove(precioDB);
                }

                var course = await _context.Course.FindAsync(request.Id);

                if (course == null)
                {
                    // throw new Exception("Dont delete this course");
                    throw new HandlerException(HttpStatusCode.NotFound, new { message = "Don't found course" });
                }

                _context.Remove(course);
                var result = await _context.SaveChangesAsync();

                if (result > 0)
                {
                    return(Unit.Value);
                }
                throw new Exception("Someting wrong, dont save changes");
            }
Ejemplo n.º 4
0
            public async Task <Unit> Handle(Execute request, CancellationToken cancellationToken)
            {
                var comment = new Comment {
                    CommentId    = Guid.NewGuid(),
                    Alumn        = request.Alumn,
                    Score        = request.Score,
                    TextComment  = request.Comment,
                    CourseId     = request.CourseId,
                    CreationDate = DateTime.UtcNow
                };

                _context.Comment.Add(comment);

                var results = await _context.SaveChangesAsync();

                if (results > 0)
                {
                    return(Unit.Value);
                }

                throw new Exception("Dont insert comment");
            }
Ejemplo n.º 5
0
        public static async Task SeedAsync(CoursesOnlineContext context, ILoggerFactory loggerFactory)
        {
            try
            {
                if (!context.Course.Any())
                {
                    var coursesData = File.ReadAllText("../Infrastructure/Data/SeedData/brands.json");

                    var courses = JsonSerializer.Deserialize <List <Course> >(coursesData);

                    foreach (var course in courses)
                    {
                        context.Course.Add(course);
                    }

                    await context.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <CoursesOnlineSeed>();
                logger.LogError(ex.Message);
            }
        }
Ejemplo n.º 6
0
            public async Task <Unit> Handle(Execute request, CancellationToken cancellationToken)
            {
                var course = await _context.Course.FindAsync(request.CourseId);

                if (course == null)
                {
                    // throw new Exception("That course dont exists");
                    throw new HandlerException(HttpStatusCode.NotFound, new { message = "Don't found course" });
                }

                //Update list of instructors of course
                course.Title             = request.Title ?? course.Title;
                course.Description       = request.Description ?? course.Description;
                course.DateOfPublication = request.DateOfPublication ?? course.DateOfPublication;

                /*Update price of course*/
                var priceEntity = _context.Price
                                  .Where(x => x.CourseId == course.CourseId)
                                  .FirstOrDefault();

                if (priceEntity != null)
                {
                    priceEntity.Promotion   = request.Promotion ?? priceEntity.Promotion;
                    priceEntity.ActualPrice = request.Price ?? priceEntity.ActualPrice;
                }
                else
                {
                    priceEntity = new Price {
                        PriceId     = Guid.NewGuid(),
                        ActualPrice = request.Price ?? 0,
                        Promotion   = request.Promotion ?? 0,
                        CourseId    = course.CourseId
                    };
                    await _context.Price.AddAsync(priceEntity);
                }

                if (request.ListInstructor != null)
                {
                    if (request.ListInstructor.Count > 0)
                    {
                        //Delete current instructors at course of DB
                        var instructorsDB = _context.CourseInstructor.Where(x => x.CourseId == request.CourseId).ToList();
                        foreach (var instructorDelete in instructorsDB)
                        {
                            _context.CourseInstructor.Remove(instructorDelete);
                        }

                        //add instructors
                        foreach (var ids in request.ListInstructor)
                        {
                            var newInstructor = new CourseInstructor {
                                CourseId     = request.CourseId,
                                InstructorId = ids
                            };
                            await _context.CourseInstructor.AddAsync(newInstructor);
                        }
                    }
                }
                var result = await _context.SaveChangesAsync();

                if (result > 0)
                {
                    return(Unit.Value);
                }
                throw new Exception("Dont save changes at course");
            }