Exemplo n.º 1
0
            public async Task <Unit> Handle(nuevo request, CancellationToken cancellationToken)
            {
                int _cursoId = await _context.Cursos.OrderByDescending(p => p.Idcurso).Select(r => r.Idcurso).FirstOrDefaultAsync();

                int _precioId = await _context.Precios.OrderByDescending(p => p.PrecioId).Select(r => r.PrecioId).FirstOrDefaultAsync();

                _cursoId++;
                _precioId++;
                var insertar = new Curso
                {
                    Idcurso          = _cursoId,
                    Nombre           = request.Nombre,
                    Descripcion      = request.Descripcion,
                    FechaPublicacion = request.FechaPublicacion
                };

                _context.Cursos.Add(insertar);

                if (request.listaInstructor != null)
                {
                    foreach (var id in request.listaInstructor)
                    {
                        var cursoInstructor = new Cursoinstructor
                        {
                            Idcurso      = _cursoId,
                            Idinstructor = id
                        };
                        _context.Cursoinstructors.Add(cursoInstructor);
                    }
                }

                //insertar precio de un curso
                var precioEntidad = new Precio
                {
                    PrecioId     = _precioId,
                    CursoId      = _cursoId,
                    PrecioActual = request.Precio,
                    Promocion    = request.Promocion
                };

                _context.Precios.Add(precioEntidad);
                var valor = await _context.SaveChangesAsync();

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

                throw new Exception("Error al agregar curso");
            }
Exemplo n.º 2
0
            public async Task <Unit> Handle(Ejecuta request, CancellationToken cancellationToken)
            {
                var InsturctorDB = _context.Cursoinstructors.Where(x => x.Idcurso == request.Idcurso);

                foreach (var instructor in InsturctorDB)
                {
                    _context.Cursoinstructors.Remove(instructor);
                }

                var comentarioDB = _context.Comentarios.Where(x => x.CursoId == request.Idcurso);

                foreach (var item in comentarioDB)
                {
                    _context.Comentarios.Remove(item);
                }

                var precioDB = _context.Precios.Where(x => x.CursoId == request.Idcurso).FirstOrDefault();

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

                var eliminaCurso = await _context.Cursos.FindAsync(request.Idcurso);

                if (eliminaCurso == null)
                {
                    //throw new Exception("No existe el registro");
                    throw new ManejadorExcepcion(HttpStatusCode.NotFound, new { Curso = "El registro no existe" });
                }

                _context.Remove(eliminaCurso);

                var resultado = await _context.SaveChangesAsync();

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

                //throw new Exception("No se pudeiron eleminar los registros");
                throw new ManejadorExcepcion(HttpStatusCode.BadRequest, new { Curso = "No se pudieron eliminar los registros" });
            }
Exemplo n.º 3
0
            public async Task <Unit> Handle(Ejecuta request, CancellationToken cancellationToken)
            {
                int _precioId = await _context.Precios.OrderByDescending(p => p.PrecioId).Select(r => r.PrecioId).FirstOrDefaultAsync();

                _precioId++;


                var curso = await _context.Cursos.FindAsync(request.Idcurso);

                if (curso == null)
                {
                    //throw new Exception("No existe el registro");
                    throw new ManejadorExcepcion(HttpStatusCode.NotFound, new { Curso = "El registro no existe" });
                }

                curso.Nombre           = request.Nombre ?? curso.Nombre;
                curso.Descripcion      = request.Descripcion ?? curso.Descripcion;
                curso.FechaPublicacion = request.FechaPublicacion ?? curso.FechaPublicacion;

                /*Actualizar precio del curso*/
                var precioEntidad = await _context.Precios.Where(x => x.CursoId == curso.Idcurso).FirstOrDefaultAsync();

                if (precioEntidad != null)
                {
                    precioEntidad.Promocion    = request.Promocion ?? precioEntidad.Promocion;
                    precioEntidad.PrecioActual = request.Precio ?? precioEntidad.PrecioActual;
                }
                else
                {
                    precioEntidad = new Precio
                    {
                        PrecioId     = _precioId,
                        PrecioActual = request.Precio ?? 0,
                        Promocion    = request.Promocion ?? 0,
                        CursoId      = curso.Idcurso
                    };
                    await _context.Precios.AddAsync(precioEntidad);
                }

                if (request.ListaInstructor != null)
                {
                    if (request.ListaInstructor.Count > 0)
                    {
                        /*Eliminar instructores*/
                        var instructoresBD = _context.Cursoinstructors.Where(x => x.Idcurso == request.Idcurso).ToList();
                        foreach (var instructorEliminar in instructoresBD)
                        {
                            _context.Cursoinstructors.Remove(instructorEliminar);
                        }


                        /*Agregar instructores de clientes*/
                        foreach (var id in request.ListaInstructor)
                        {
                            var nuevoInstructor = new Cursoinstructor
                            {
                                Idcurso      = request.Idcurso,
                                Idinstructor = id
                            };
                            _context.Cursoinstructors.Add(nuevoInstructor);
                        }
                    }
                }


                var resultado = await _context.SaveChangesAsync();

                if (resultado > 0)
                {
                    return(Unit.Value);
                }
                else
                {
                    throw new Exception("No se pudo editar");
                }
            }