public void AddProduct(IProductDto productDto) { SqlConnection conn = webShopContext.GetConnection(); try { conn.Open(); string sql = "INSERT INTO product (name, description, price, image_path, category_id)" + "VALUES(@name, @description, @price, @imagePath, @categoryId);"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@name", productDto.Name); cmd.Parameters.AddWithValue("@description", productDto.Description); cmd.Parameters.AddWithValue("@price", productDto.Price); cmd.Parameters.AddWithValue("@imagePath", productDto.ImagePath ?? (object)DBNull.Value); cmd.Parameters.AddWithValue("@categoryId", productDto.CategoryID); cmd.ExecuteNonQuery(); } catch (Exception e) { throw e; } conn.Close(); }
public void UpdateProduct(IProductDto productDto) { SqlConnection conn = webShopContext.GetConnection(); try { conn.Open(); string sql = "UPDATE product SET name = @name, description = @description, " + "price = @price, image_path = @imagePath, category_id = @categoryId WHERE id = @productId;"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@name", productDto.Name); cmd.Parameters.AddWithValue("@description", productDto.Description); cmd.Parameters.AddWithValue("@price", productDto.Price); cmd.Parameters.AddWithValue("@imagePath", productDto.ImagePath ?? (object)DBNull.Value); cmd.Parameters.AddWithValue("@productId", productDto.ProductID); cmd.Parameters.AddWithValue("@categoryId", productDto.CategoryID); cmd.ExecuteNonQuery(); } catch (Exception e) { throw e; } conn.Close(); }
public bool AddProduct(IProductDto product) { var productId = GetProducts().Select(e => e.ProductId).Max() + 1; var newProduct = new Product { ProductId = productId, LastUpdated = DateTime.UtcNow, Name = product.Name, Price = product.Price, Sku = product.Sku }; if (!string.IsNullOrEmpty(product.Category)) { var category = ProductDbContext.Categories.FirstOrDefault(e => e.Name.Equals(product.Category)); if (category != null) { newProduct.CategoryId = category.Id; } } var addedProduct = ProductDbContext.Products.Add(newProduct); if (addedProduct != null && addedProduct.Id != 0) { return ProductDbContext.SaveChanges() == 1; } return false; }
public ProductRetrievedEvent(GetProductDto product) { if (product != null) { ProductDto = new GetProductDto(product.Id, product.Code, product.Name, product.Description, product.Photo, product.PhotoName, product.Price, product.LastUpdated, product.BlobName); } }
private IProduct ProductDtoToProduct(IProductDto productDto) { return(new Product() { ProductID = productDto.ProductID, Name = productDto.Name, Description = productDto.Description, Price = productDto.Price, ImagePath = productDto.ImagePath, CategoryId = productDto.CategoryID, }); }
private IProductDto ProductToProductDto(IProduct product) { IProductDto productDto = Factory.GetProductDto(); productDto.ProductID = product.ProductID; productDto.Name = product.Name; productDto.Description = product.Description; productDto.Price = product.Price; productDto.ImagePath = product.ImagePath; productDto.CategoryID = product.CategoryId; return(productDto); }
public async Task <IProductDto> ModifyProductAsync(IProductDto productDto) { if (productDto == null) { throw new ArgumentNullException(nameof(productDto)); } var productPoco = this.mapper.Map <ProductPoco>(productDto); if (!await this.productDataAccess.ExistsProductAsync(productPoco.CustomerId, productPoco.Id)) { throw new ArgumentException("Product doesn't exist.", nameof(productDto)); } var savedProductPoco = await this.productDataAccess.UpdateProductAsync(productPoco); this.logger.LogInformation(LoggingEvents.UpdateItem, $"{nameof(this.ModifyProductAsync)}: successful [Id: {savedProductPoco.Id}]"); var savedProductDto = this.mapper.Map <ProductDto>(savedProductPoco); return(savedProductDto); }
public IProductDto GetProductById(int id) { SqlConnection conn = webShopContext.GetConnection(); IProductDto product = null; try { conn.Open(); string sql = "SELECT id, name, description, price, image_path, category_id FROM product WHERE id = @id;"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@id", id); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { product = new ProductDto() { ProductID = rdr.GetInt32(0), Name = rdr.GetString(1), Description = rdr.GetString(2), Price = rdr.GetDecimal(3), ImagePath = !rdr.IsDBNull(4) ? rdr.GetString(4) : null, CategoryID = rdr.GetInt32(5), }; } rdr.Close(); } catch (Exception e) { throw e; } conn.Close(); return(product); }
public async Task <IProductDto> AddProductAsync(IProductDto productDto) { if (productDto == null) { new ArgumentNullException(nameof(productDto)); } if (productDto.Id > 0) { new ArgumentException("Product has already an id.", nameof(productDto)); } //TODO: Prüfen ob customer vorhanden ist. var productPoco = this.mapper.Map <ProductPoco>(productDto); var savedProductPoco = await this.productDataAccess.InsertProductAsync(productPoco); this.logger.LogInformation(LoggingEvents.InsertItem, $"{nameof(this.AddProductAsync)}: successful [Id: {savedProductPoco.Id}]"); var savedProductDto = this.mapper.Map <ProductDto>(savedProductPoco); return(savedProductDto); }
public async Task <bool> DeleteProductAsync(IProductDto productDto) { if (productDto == null) { throw new ArgumentNullException(nameof(productDto)); } var productPoco = this.mapper.Map <ProductPoco>(productDto); if (!await this.productDataAccess.ExistsProductAsync(productPoco.CustomerId, productPoco.Id)) { return(false); } var successful = await this.productDataAccess.RemoveProductAsync(productPoco.Id); if (successful) { this.logger.LogInformation(LoggingEvents.DeleteItem, $"{nameof(this.DeleteProductAsync)}: successful [Id: {productPoco.Id}]"); } return(successful); }
public ProductResponse(IProductDto productDto) { Uid = productDto.Uid; Name = productDto.Name; }
private string ProductToString(IProductDto product) { return($"{product.Id}\t{product.ProductName}\t{product.CategoryId}" + $"\tPrice: {_currencyService.ToUserCurrency(CurrencyMatrixData.CurrencyMockRate[_currencyCode - 1], product.Price)}"); }