public void BuyFromDistributor(AddingFoodServiceModel model) { if (String.IsNullOrWhiteSpace(model.Name)) { throw new ArgumentException("Name cannot be null or whitespace!"); } // Profit should be in range 0-500% if (model.Profit < 0 || model.Profit > 5) { throw new ArgumentException("Profit must be higher than zero and lowe than 500%"); } Food food = new Food() { Name = model.Name, Weight = model.Weight, DistributorPrice = model.Price, Price = model.Price + (model.Price * (decimal)model.Profit), BrandId = model.BrandId, CategoryId = model.CategoryId, }; this.context.Foods.Add(food); this.context.SaveChanges(); }
public void Buy(AddingFoodServiceModel model) { if (string.IsNullOrWhiteSpace(model.Name)) { throw new ArgumentException("Name cannot be null or whitespace!"); } if (model.Profit < 0 || model.Profit > 5) { throw new ArgumentException("Profit must be higher than 0% and lower than 500%"); } var food = new Food() { Name = model.Name, Weight = model.Weight, DistributorPrice = model.Price, Price = model.Price + (model.Price * (decimal)model.Profit), BrandId = model.BrandId, CategoryId = model.CategoryId }; this.data.Foods.Add(food); this.data.SaveChanges(); }
public void BuyFromDistributor(AddingFoodServiceModel model) { if (String.IsNullOrWhiteSpace(model.Name)) { throw new ArgumentException("Name cannot be null or whitespace"); } if (model.Profit < 0) { throw new ArgumentException("Profit must be figher than zero"); } var food = new Food() { Name = model.Name, Weigth = model.Weight, DistributorPrice = model.Price, Price = model.Price + (model.Price * model.Profit), ExpirationDate = model.ExpirationDate, BrandId = model.BrandId, CategoryId = model.CategoryId }; this.data.Food.Add(food); this.data.SaveChanges(); }
public void BuyFromDistributor(AddingFoodServiceModel model) { if (String.IsNullOrEmpty(model.Name)) { throw new InvalidOperationException("Name cannot be empty!"); } if (model.Price <= 0) { throw new InvalidOperationException("Price cannot be less than 0 or equal to 0!"); } if (model.Profit < 0 || model.Profit > 1) { throw new InvalidOperationException("Profit must be between 0 and 1!"); } if (String.IsNullOrEmpty(model.BrandName)) { throw new InvalidOperationException("Brand Name cannot be empty!"); } if (String.IsNullOrEmpty(model.CategoryName)) { throw new InvalidOperationException("Category Name cannot be empty!"); } var brand = this.brandService .SearchByName(model.BrandName) .FirstOrDefault(); if (brand == null) { throw new InvalidOperationException("Brand with this name not exist!"); } var category = this.categoryService .SearchByName(model.CategoryName); if (category == null) { throw new InvalidOperationException("Category with this name not exist!"); } var food = new Food() { Name = model.Name, Weight = model.Weight, DistributorPrice = model.Price, Price = model.Price + (model.Price * (decimal)model.Profit), BrandId = brand.Id, CategoryId = category.Id }; this.context.Foods.Add(food); this.context.SaveChanges(); }
public void BuyFromDistributor(AddingFoodServiceModel model) { var food = new Food() { Name = model.Name, Weight = model.Weight, DistributorPrice = model.Price, Price = model.Price + (model.Price * model.Profit), BrandId = model.BrandId, CategoryId = model.CategoryId }; this.data.Food.Add(food); this.data.SaveChanges(); }
public void BuyFromDistributor(AddingFoodServiceModel model) { ValidateName(model.Name); ValidateProfit(model.Profit); var food = new Food() { Name = model.Name, Weight = model.Weight, DistributorPrice = model.Price, Price = model.Price + (model.Profit * model.Price), ExpirationDate = model.ExpirationDate, BrandId = model.BrandId, CategoryId = model.CategoryId }; this.data.Foods.Add(food); this.data.SaveChanges(); }
public int BuyFromDistributor(AddingFoodServiceModel model) { ValidateFood(model.Name, model.BrandId, model.CategoryId, model.Weight, model.Price); var food = new Food() { Name = model.Name, Weight = model.Weight, Price = model.Price, ExpirationDate = model.ExpirationDate, BrandId = model.BrandId, CategoryId = model.CategoryId }; this.data.Food.Add(food); this.data.SaveChanges(); return(food.Id); }
public void BuyFromDistributor(AddingFoodServiceModel model) { if (this.data.Brands.Any(b => b.Id == model.BrandId) == false) { throw new InvalidOperationException(OutputMessages.InvalidBrand); } else if (this.data.Categories.Any(c => c.Id == model.CategoryId) == false) { throw new InvalidOperationException(OutputMessages.InvalidCategory); } var food = CreateFood(model.Name, model.Weight, model.Price, model.Profit, model.Quantity, model.ExpirationDate, model.BrandId, model.CategoryId); if (IsValid(food) == false) { throw new InvalidOperationException(OutputMessages.InvalidFood); } this.data.Foods.Add(food); this.data.SaveChanges(); }
public static void Main() { using var context = new PetStoreDbContext(); var brandService = new BrandService(context); var categoryService = new CategoryService(context); var foodService = new FoodService(context, brandService, categoryService); var addingFoodModel = new AddingFoodServiceModel() { Name = "Banana", Weight = 5, Price = 6m, Profit = 0.4, ExpiryDate = DateTime.Now, BrandName = "Bevola", CategoryName = "Food" // error, because not exist }; foodService.BuyFromDistributor(addingFoodModel); }