Example #1
0
            public async Task <Unit> Handle(Ejecuta request, CancellationToken cancellationToken)
            {
                Guid _cursoId = Guid.NewGuid();
                var  curso    = new Curso
                {
                    CursoId          = _cursoId,
                    Titulo           = request.Titulo,
                    Descripcion      = request.Descripcion,
                    FechaPublicacion = request.FechaPublicacion,
                    FechaCreacion    = DateTime.UtcNow
                };

                _contextSeed.Curso.Add(curso);

                if (request.ListaInstructor != null)
                {
                    foreach (var id in request.ListaInstructor)
                    {
                        var cursoInstructor = new CursoInstructor
                        {
                            CursoId      = _cursoId,
                            InstructorId = id
                        };
                        _contextSeed.CursoInstructor.Add(cursoInstructor);
                    }
                }
                //agregar logica para insertar un precio del curso.

                var precioEntidad = new Precio
                {
                    CursoId         = _cursoId,
                    PrecioActual    = request.Precio,
                    PrecioPromocion = request.Promocion,
                    PrecioId        = Guid.NewGuid()
                };

                _contextSeed.Precio.Add(precioEntidad);

                //guardando en la BD (savechangesasync)
                var valor = await _contextSeed.SaveChangesAsync();

                if (valor > 0)
                {
                    return(Unit.Value);
                }
                throw new Exception("No se puedo insertar el curso");
            }
Example #2
0
            public async Task <Unit> Handle(Ejecuta request, CancellationToken cancellationToken)
            {
                var comentario = await _contextSeed.Comentario.FindAsync(request.Id);

                if (comentario == null)
                {
                    throw new ManejadorExcepcion(HttpStatusCode.NotFound, new { mensaje = "no se encontro el comentario" });
                }
                _contextSeed.Remove(comentario);
                var resultado = await _contextSeed.SaveChangesAsync();

                if (resultado > 0)
                {
                    return(Unit.Value);
                }
                throw new Exception("No se puedo eliminar el comentario");
            }
Example #3
0
            public async Task <Unit> Handle(Ejecuta request, CancellationToken cancellationToken)
            {
                var instructoresDB = _contextSeed.CursoInstructor.Where(x => x.CursoId == request.Id);

                foreach (var instructor in instructoresDB)
                {
                    _contextSeed.CursoInstructor.Remove(instructor);
                }
                //eliminar precio y comentarios
                var comentariosDb = _contextSeed.Comentario.Where(x => x.CursoId == request.Id);

                foreach (var cmt in comentariosDb)
                {
                    _contextSeed.Comentario.Remove(cmt);
                }
                var precioDB = _contextSeed.Precio.Where(x => x.CursoId == request.Id).FirstOrDefault();

                if (precioDB != null)
                {
                    _contextSeed.Precio.Remove(precioDB);
                }



                var curso = await _contextSeed.Curso.FindAsync(request.Id);

                if (curso == null)
                {
                    throw new ManejadorExcepcion(HttpStatusCode.NotFound, new { curso = "No se encontro el Curso" });
                }
                _contextSeed.Remove(curso);

                var resultado = await _contextSeed.SaveChangesAsync();

                if (resultado > 0)
                {
                    return(Unit.Value);
                }
                throw new Exception("no se guardo los cambios");
            }
Example #4
0
            public async Task <Unit> Handle(Ejecuta request, CancellationToken cancellationToken)
            {
                var comentario = new Comentario
                {
                    ComentarioId    = Guid.NewGuid(),
                    Alumno          = request.Alumno,
                    Puntaje         = request.Puntaje,
                    ComentarioTexto = request.ComentarioTexto,
                    CursoId         = request.CursoId,
                    FechaCreacion   = DateTime.UtcNow
                };

                _contextSeed.Comentario.Add(comentario);

                var resultado = await _contextSeed.SaveChangesAsync();

                if (resultado > 0)
                {
                    return(Unit.Value);
                }
                throw new Exception("No se pudo insertar el comentario");
            }
Example #5
0
            public async Task <Unit> Handle(Ejecuta request, CancellationToken cancellationToken)
            {
                var curso = await _contextSeed.Curso.FindAsync(request.CursoId);

                if (curso == null)
                {
                    throw new ManejadorExcepcion(HttpStatusCode.NotFound, new { curso = "No se encontro el Curso" });
                }

                curso.Titulo           = request.Titulo ?? curso.Titulo;
                curso.Descripcion      = request.Descripcion ?? curso.Descripcion;
                curso.FechaPublicacion = request.FechaPublicacion ?? curso.FechaPublicacion;
                curso.FechaCreacion    = DateTime.UtcNow;

                ////actualizar el precio del curso
                var precioEntidad = _contextSeed.Precio.Where(x => x.CursoId == curso.CursoId).FirstOrDefault();

                if (precioEntidad != null)
                {
                    precioEntidad.PrecioPromocion = request.Promocion ?? precioEntidad.PrecioPromocion;
                    precioEntidad.PrecioActual    = request.Precio ?? precioEntidad.PrecioActual;
                }
                else
                {
                    precioEntidad = new Precio
                    {
                        PrecioId        = Guid.NewGuid(),
                        PrecioActual    = request.Precio ?? 0,
                        PrecioPromocion = request.Promocion ?? 0,
                        CursoId         = curso.CursoId
                    };
                    await _contextSeed.Precio.AddAsync(precioEntidad);
                }



                if (request.ListaInstructor != null)
                {
                    if (request.ListaInstructor.Count > 0)
                    {//eliminar los instructores actuales.
                        var instructoresBD = _contextSeed.CursoInstructor.Where(x => x.CursoId == request.CursoId);
                        foreach (var instructorEliminar in instructoresBD)
                        {
                            _contextSeed.CursoInstructor.Remove(instructorEliminar);
                        }//fin
                        //agregar instructor que viene del cliente
                        foreach (var id in request.ListaInstructor)
                        {
                            var nuevoInstructor = new CursoInstructor
                            {
                                CursoId      = request.CursoId,
                                InstructorId = id
                            };
                            _contextSeed.CursoInstructor.Add(nuevoInstructor);
                        }
                        //fin
                    }
                }
                var resultado = await _contextSeed.SaveChangesAsync();

                if (resultado > 0)
                {
                    return(Unit.Value);
                }
                throw new Exception("no se guardaron los cambios en el curso.");
            }