public ModelLibrary.Item CreateItem(ModelLibrary.Item item, int menuId, int itemCatId) { var itemDb = new ItemDb(); var dbItem = new DatabaseAccessLibrary.Item { name = item.Name, menuId = menuId, description = item.Description, itemCatId = itemCatId, }; var modelItem = new ModelLibrary.Item { Name = item.Name, Description = item.Description, }; var context = new ValidationContext(item, null, null); var result = new List <ValidationResult>(); var isModelStateValid = Validator.TryValidateObject(item, context, result, true); if (!isModelStateValid) { throw new ValidationException(); } itemDb.AddItem(dbItem); return(modelItem); }
public DatabaseAccessLibrary.Item ConvertItemToDb(ModelLibrary.Item modelItem, int menuId, int itemCatId) { JustFeastDbDataContext db = new JustFeastDbDataContext(); //var CheckItem = db.Items.Single(a => a.id == modelItem.Id); var dbItem = new DatabaseAccessLibrary.Item(); var menu = db.Menus.SingleOrDefault(p => p.id == menuId); var itemCat = db.ItemCats.SingleOrDefault(p => p.id == itemCatId); if (menu != null && itemCat != null && modelItem != null) { dbItem = new DatabaseAccessLibrary.Item { id = modelItem.Id, description = modelItem.Description, itemCatId = itemCatId, menuId = menuId, name = modelItem.Name, }; } return(dbItem); }
public ModelLibrary.Item ConvertItemToModel(DatabaseAccessLibrary.Item dbItem) { var db = new JustFeastDbDataContext(); var priceCtrl = new PriceCtrl(); var price = db.Prices.Where(p => p.itemId == dbItem.id).OrderByDescending(p => p.startDate).FirstOrDefault(); if (dbItem != null && price != null) { var modelItem = new ModelLibrary.Item { Id = dbItem.id, Name = dbItem.name, Description = dbItem.description, Price = priceCtrl.ConvertPriceToModel(price) }; return(modelItem); } else { return(null); } }
public ModelLibrary.Item ConvertItemToModelWithoutPrice(DatabaseAccessLibrary.Item dbItem) { if (dbItem != null) { var modelItem = new ModelLibrary.Item { Id = dbItem.id, Name = dbItem.name, Description = dbItem.description, }; return(modelItem); } else { var modelItem = new ModelLibrary.Item { Id = dbItem.id, Name = dbItem.name, Description = dbItem.description, }; return(modelItem); } }