public void Add_ValidObjectPassed_ReturnOk()
        {
            var _service    = new Mock <SolicitudService>();
            var _controller = new SolicitudController(_service.Object);

            // Arrange
            SolicitudCreateDto modelCreateDto = new SolicitudCreateDto
            {
                Dni           = "15665105",
                FamiliaNombre = "LosGeniales"
            };
            SolicitudDto modelDto = new SolicitudDto
            {
                Dni           = "15665105",
                FamiliaNombre = "LosGeniales"
            };

            _service.Setup(x => x.Create(modelCreateDto)).Returns(modelDto);
            _service.Setup(x => x.AceptaSolicitudes(modelCreateDto)).Returns(true);

            // Act
            ActionResult result = _controller.Post(modelCreateDto);

            // Assert
            //Assert.IsType<OkObjectResult>(result);
        }
        public SolicitudDto Create(SolicitudCreateDto model)
        {
            Solicitud entry = null;

            if (_context.Familias.SingleOrDefault(x => x.Nombre == model.FamiliaNombre) == null)
            {
                throw new FamilyNotFoundException();
            }

            else
            {
                if (AceptaSolicitudes(model) == false)
                {
                    throw new FamilyNotAceptedSolicitudeException();
                }
                else
                {
                    var familia = _context.Familias.Single(x => x.Nombre == model.FamiliaNombre);

                    entry = new Solicitud
                    {
                        FamiliaId = familia.FamiliaId,
                        Dni       = model.Dni,
                    };

                    _context.Add(entry);
                    _context.SaveChanges();
                }
            }
            return(_mapper.Map <SolicitudDto>(entry));
        }
        public void Add_ValidObjectPassed_ReturnBadRequest()
        {
            var _service    = new Mock <SolicitudService>();
            var _controller = new SolicitudController(_service.Object);

            // Arrange
            SolicitudCreateDto modelCreateDto = new SolicitudCreateDto
            {
                Dni           = "76697298",
                FamiliaNombre = "Los Pollitos"
            };

            SolicitudDto modelDto = new SolicitudDto
            {
                Dni           = "76697298",
                FamiliaNombre = "Los Pollitos"
            };

            _service.Setup(x => x.Create(modelCreateDto)).Returns(modelDto);
            _service.Setup(x => x.AceptaSolicitudes(modelCreateDto)).Returns(false);

            // Act
            ActionResult result = _controller.Post(modelCreateDto);

            // Assert
            //Assert.IsType<BadRequestObjectResult>(result);
        }
 public bool AceptaSolicitudes(SolicitudCreateDto model)
 {
     if (_context.Familias.Single(x => x.Nombre == model.FamiliaNombre).AceptaSolicitudes == true)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
예제 #5
0
 public ActionResult Post(SolicitudCreateDto model)
 {
     try
     {
         return(Created("Created", _solicitudService.Create(model)));
     }
     catch (FamilyNotFoundException FamilyNotFoundException)
     {
         return(BadRequest(FamilyNotFoundException.ExceptionDto));
     }
     catch (FamilyNotAceptedSolicitudeException FamilyNotAceptedSolicitudeException)
     {
         return(BadRequest(FamilyNotAceptedSolicitudeException.ExceptionDto));
     }
 }
        public void Add_InvalidObjectPassed_ReturnNotFound()
        {
            var _service    = new Mock <SolicitudService>();
            var _controller = new SolicitudController(_service.Object);

            // Arrange
            SolicitudCreateDto modelCreateDto = new SolicitudCreateDto
            {
                Dni           = "76697297",
                FamiliaNombre = "Los pinguinos"
            };

            SolicitudDto modelDto = null;

            _service.Setup(x => x.Create(modelCreateDto)).Returns(modelDto);

            // Act
            ActionResult result = _controller.Post(modelCreateDto);

            // Assert
            //Assert.IsType<NotFoundObjectResult>(result);
        }
예제 #7
0
 public async Task <IActionResult> Create([FromBody] SolicitudCreateDto model)
 {
     return(Ok(
                await _solicitud.CrearSolicitud(model)
                ));
 }
예제 #8
0
        //insertar o actualizar empleado
        public async Task <bool> CrearSolicitud(SolicitudCreateDto model)
        {
            var result = false;

            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    var solicitud = new Solicitud
                    {
                        Id                  = model.Id,
                        EmpleadoId          = model.EmpleadoId,
                        Codigo              = model.Codigo,
                        No_Orden            = model.No_Orden,
                        No_Serie            = model.No_Serie,
                        Codigo_Programatico = model.Codigo_Programatico,
                        Fecha_Solicitud     = model.Fecha_solicitud,
                        Fecha_Entrega       = model.Fecha_Entrega,
                        Tipo                = model.Tipo,
                        Estado_Solicitud    = model.Estado_Solicitud,
                        Observaciones       = model.Observaciones,
                        Total               = model.Total
                    };

                    if (solicitud.Id > 0)
                    {
                        _context.Solicitud.Update(solicitud);
                    }
                    else
                    {
                        _context.Solicitud.Add(solicitud);
                    }

                    await _context.SaveChangesAsync();

                    int solicitudId = solicitud.Id;
                    try
                    {
                        foreach (var d in model.detalle)
                        {
                            var detalle = new DetalleSolicitud
                            {
                                Id                  = d.Id,
                                SolicitudId         = solicitudId,
                                Cantidad_Solicitada = d.Cantidad_Solicitada,
                                Cantidad_Entregada  = d.Cantidad_Entregada,
                                UnidadMedida        = d.UnidadMedida,
                                ProductoId          = d.ProductoId
                            };

                            if (detalle.Id > 0)
                            {
                                _context.DetalleSolicitud.Update(detalle);
                            }
                            else
                            {
                                _context.DetalleSolicitud.Add(detalle);
                            }

                            await _context.SaveChangesAsync();

                            //si cantidad entregada es mayor que cero
                            if (d.Cantidad_Entregada > 0)
                            {
                                var soli = await _context.Solicitud.Where(x => x.Id == d.SolicitudId).SingleOrDefaultAsync();

                                if (soli != null)
                                {
                                    soli.Fecha_Entrega = DateTime.Today;
                                    _context.Solicitud.Update(soli);
                                }
                                var producto = await _context.Almacen.Where(x => x.ProductoId == d.ProductoId).SingleOrDefaultAsync();

                                if (producto != null)
                                {
                                    producto.Stock = producto.Stock - d.Cantidad_Entregada;
                                    _context.Almacen.Update(producto);

                                    await _context.SaveChangesAsync();
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                    transaction.Commit();

                    result = true;
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    result = false;
                    throw e;
                }
            }

            return(result);
        }