public async Task <IActionResult> AddProductorAsync(Productor productor)
        {
            if (productor == null || string.IsNullOrWhiteSpace(productor.Identificacion))
            {
                return(BadRequest());
            }

            var exists = await JsonHandler.CheckIfExists(FilePath.Productores, productor, (p1, p2) =>
            {
                return(p1.Identificacion == p2.Identificacion);
            });

            if (exists)
            {
                return(Conflict());
            }

            //Los productores agregados desde la vista de administración están afiliados por defecto
            productor.Afiliado = true;
            productor.Password = Encryption.Encrypt(productor.Password);

            await JsonHandler.AddToFileAsync(FilePath.Productores, productor)
            .ConfigureAwait(false);

            //return CreatedAtRoute("GetProductor", productor);
            return(CreatedAtRoute("default", new { id = productor.Identificacion }, productor));
        }
Exemple #2
0
        public async Task <IActionResult> AddCategoriaAsync(Categoria categoria)
        {
            if (categoria == null || string.IsNullOrWhiteSpace(categoria.Nombre))
            {
                return(BadRequest());
            }

            var exists = await JsonHandler.CheckIfExists(FilePath.Categorias, categoria, (c1, c2) =>
            {
                return(c1.Nombre == c2.Nombre);
            });

            if (exists)
            {
                return(Conflict());
            }

            var categorias = await JsonHandler.LoadFileAsync <Categoria>(FilePath.Categorias);

            //Id de categoría autoincrementable
            categoria.Id = Math.Abs(categoria.Nombre.GetHashCode());

            await JsonHandler.AddToFileAsync(FilePath.Categorias, categoria);

            return(CreatedAtRoute("default", new { id = categoria.Id }, categoria));
        }
Exemple #3
0
        public async Task <IActionResult> AddAfiliacionAsync(Productor productor)
        {
            if (productor == null || string.IsNullOrWhiteSpace(productor.Identificacion))
            {
                return(BadRequest());
            }

            var productorExists = await JsonHandler.CheckIfExists <Productor>(FilePath.Productores, productor, (p1, p2) =>
            {
                return(p1.Identificacion == p2.Identificacion);
            });

            if (productorExists)
            {
                return(Conflict());
            }

            var afiliaciones = await JsonHandler.LoadFileAsync <Afiliacion>(FilePath.Afiliaciones);

            var afiliacion = new Afiliacion()
            {
                Id          = afiliaciones.Count,
                Estado      = AfiliacionStatus.Pendiente,
                IdProductor = productor.Identificacion
            };

            /**
             * Al crear una solicitud de afiliación, el productor no va a estar afiliado
             * hasta que la solicitud sea aceptada o denegada manualmente
             */
            productor.Afiliado = false;

            productor.Password = Encryption.Encrypt(productor.Password);

            afiliaciones.Add(afiliacion);

            await JsonHandler.OvewriteFileAsync(FilePath.Afiliaciones, afiliaciones);

            await JsonHandler.AddToFileAsync(FilePath.Productores, productor);

            return(CreatedAtRoute("default", new { id = afiliacion.Id }, afiliacion));
        }
Exemple #4
0
        public async Task <IActionResult> AddCarritoAsync(string idCliente, Carrito carrito)
        {
            //TODO: verificar que el cliente exista
            if (string.IsNullOrWhiteSpace(idCliente))
            {
                return(BadRequest());
            }

            var exists = await JsonHandler.CheckIfExists(FilePath.Carrito, carrito, (c1, c2) =>
            {
                return(c1.IdCliente == c2.IdCliente && c1.IdProducto == c2.IdProducto);
            });

            if (exists)
            {
                return(Conflict());
            }

            var productos = await JsonHandler.LoadFileAsync <Producto>(FilePath.Productos);

            var producto = productos.FirstOrDefault(p => p.Id == carrito.IdProducto);

            if (producto == null || carrito.Cantidad > producto.Disponibilidad)
            {
                return(BadRequest());
            }

            productos.Remove(producto);
            producto.Disponibilidad -= carrito.Cantidad;
            productos.Add(producto);

            carrito.IdCliente = idCliente;

            await JsonHandler.AddToFileAsync(FilePath.Carrito, carrito);

            return(CreatedAtRoute("default", new { id = carrito.IdProducto }, carrito));
        }
        public async Task <IActionResult> AddClienteAsync(Cliente cliente)
        {
            cliente.Password = Encryption.Encrypt(cliente.Password);

            if (cliente == null || string.IsNullOrWhiteSpace(cliente.Identificacion))
            {
                return(BadRequest());
            }

            var exists = await JsonHandler.CheckIfExists(FilePath.Clientes, cliente, (p1, p2) =>
            {
                return(p1.Identificacion == p2.Identificacion);
            });

            if (exists)
            {
                return(Conflict());
            }

            await JsonHandler.AddToFileAsync(FilePath.Clientes, cliente)
            .ConfigureAwait(false);

            return(CreatedAtRoute("default", new { id = cliente.Identificacion }, cliente));
        }