public Response Agregar(ProductoSubCategoriaRequest request) { ProductoCategoria categoria = this._unitOfWork.CategoriaRepository .FindBy(categoria => categoria.Id == request.IdCategoria, includeProperties: "SubCategorias").FirstOrDefault(); if (categoria == null) { return(new Response { Mensaje = "La categoria no fue encontrada" }); } ProductoSubCategoria subCategoria = categoria.SubCategorias .FirstOrDefault(subCategoria => subCategoria.Nombre == request.NombreSubCategoria); if (subCategoria != null) { return(new Response { Mensaje = $"La subcategoria {request.NombreSubCategoria} ya " + $"se encuentra registrada" }); } subCategoria = new ProductoSubCategoria .ProductoSubCategoriaBuilder(request.NombreSubCategoria).Build(); categoria.SubCategorias.Add(subCategoria); this._unitOfWork.CategoriaRepository.Edit(categoria); this._unitOfWork.Commit(); return(new Response { Mensaje = "Subcategoria registrada con éxito", Data = new ProductoCategoriaRequest().Map(categoria) }); }
public override Response Crear(ProductoRequest request) { var errores = ProductoPuedeCrear.PuedeCrearProducto (request.CantidadProducto, request.CostoUnitarioProducto, request.PorcentajeDeUtilidadProducto); if (errores.Any()) { return new Response { Mensaje = String.Join(", ", errores) } } ; ProductoSubCategoria subCategoria = BuscarSubCategoriaConId(request.IdSubCategoria); if (subCategoria == null) { return(new Response { Mensaje = "La subcategoria no fué encontrada" }); } Producto producto = this._unitOfWork.ProductoRepository. FindFirstOrDefault(t => t.Nombre == request.NombreProducto); if (producto != null) { return new Response { Mensaje = "El producto ya existe" } } ; try { producto = new CrearProductoFactory(request.Tipo). CrearProducto(request.Especificacion); } catch (InvalidOperationException e) { return(new Response { Mensaje = e.Message }); } var emboltorio = this._unitOfWork.ProductoRepository .FindFirstOrDefault(x => x.Id == request.IdEmboltorio); producto.SetNombre(request.NombreProducto).SetCantidad(request.CantidadProducto). SetCostoUnitario(request.CostoUnitarioProducto). SetPorcentajeDeUtilidad(request.PorcentajeDeUtilidadProducto). SetUnidadDeMedida(request.UnidadDeMedidaProducto); producto.SetEnvoltorio(emboltorio); producto.SetSubCategoria(subCategoria); this._unitOfWork.ProductoRepository.Add(producto); this._unitOfWork.Commit(); return(new Response { Mensaje = "Producto registrado con éxito", Data = new ProductoRequest().Map(producto) }); } } }