00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 using Foodolini.Database;
00006
00007 namespace Foodolini.BusinessLogic
00008 {
00012 public class ShoppingListItem
00013 {
00014 private ShoppingListItemRow row;
00015 private bool modified = false;
00016
00017 public ShoppingListItem(Ingredient ingredient, double quantity, Person owner)
00018 {
00019 row = new ShoppingListItemRow();
00020 this.Ingredient = ingredient;
00021 this.Quantity = quantity;
00022 this.Owner = owner;
00023 }
00024
00025 public ShoppingListItem(Recipe recipe, Ingredient ingredient, double quantity, Person owner)
00026 {
00027 row = new ShoppingListItemRow();
00028 this.Recipe = recipe;
00029 this.Ingredient = ingredient;
00030 this.Quantity = quantity;
00031 this.Owner = owner;
00032 }
00033
00034 internal ShoppingListItem(ShoppingListItemRow row)
00035 {
00036 this.row = row;
00037 this.modified = false;
00038 }
00039
00043 internal long Id
00044 {
00045 get { return this.row.ShoppingListItemRowId; }
00046 }
00047
00051 public Person Owner
00052 {
00053
00054 get { return Person.GetById(this.row.UserId); }
00055 set { this.row.UserId = value.Id; }
00056 }
00057
00061 private Recipe recipe;
00065 public Recipe Recipe
00066 {
00067 get {
00068
00069 if (!this.row.RecipeRowId.HasValue)
00070 return null;
00071
00072
00073 if (this.recipe == null)
00074 recipe = Recipe.GetById(this.row.RecipeRowId.Value);
00075 return this.recipe;
00076 }
00077 set
00078 {
00079 modified = true;
00080 this.row.RecipeRowId = value.Id;
00081 }
00082 }
00083
00087 public double Quantity
00088 {
00089 get { return this.row.Quantity; }
00090 set
00091 {
00092 modified = true;
00093 this.row.Quantity = value;
00094 }
00095 }
00096
00097 private Ingredient ingredient;
00101 public Ingredient Ingredient
00102 {
00103 get {
00104 if(this.ingredient==null)
00105 ingredient = Ingredient.GetById(this.row.FoodDescriptionId);
00106 return this.ingredient;
00107 }
00108 set
00109 {
00110 modified = true;
00111 this.row.FoodDescriptionId = value.Id;
00112 }
00113 }
00114
00120 static internal IEnumerable<ShoppingListItem> GetShoppingListItems(Person owner)
00121 {
00122 foreach (var row in Settings.Repo.Where<ShoppingListItemRow>(" UserId = @0 ", owner.Id)) {
00123 yield return new ShoppingListItem(row);
00124 }
00125 }
00126
00130 public void Save() {
00131 if (this.modified) {
00132 if (this.Id == 0)
00133 Settings.Repo.Add<ShoppingListItemRow>(this.row);
00134 else
00135 Settings.Repo.Update<ShoppingListItemRow>(this.row);
00136 }
00137 }
00138
00139 public void Delete()
00140 {
00141
00142 if (this.Id != 0) {
00143 Settings.Repo.Delete<ShoppingListItemRow>(this.row);
00144 }
00145 this.modified = false;
00146 }
00147
00148 }
00149 }