00001 using System; 00002 using System.Collections.Generic; 00003 using System.Text; 00004 using Foodolini.Database; 00005 00006 namespace Foodolini.BusinessLogic 00007 { 00011 public class Exercise 00012 { 00013 private ExerciseRow row; 00014 private bool modified = true; 00015 00016 public Exercise(SportsActivity activity, TimeSpan duration, Person performedBy, DateTime performedDate){ 00017 this.row = new ExerciseRow(); 00018 this.activity = activity; 00019 this.Duration = duration; 00020 this.PerformedBy = performedBy; 00021 this.PerformedDate = performedDate; 00022 this.modified = true; 00023 } 00024 00025 internal Exercise(ExerciseRow row){ 00026 this.row = row; 00027 this.modified = false; 00028 } 00029 00030 private SportsActivity activity; 00031 00035 public SportsActivity Activity 00036 { 00037 get { 00038 try{ 00039 if(this.activity == null) 00040 this.activity = new SportsActivity(this.row.SportsActivityId); 00041 return this.activity; 00042 } 00043 catch(System.Exception e){ 00044 return null; 00045 } 00046 } 00047 set { 00048 this.activity = value; 00049 this.modified = true; 00050 } 00051 } 00052 00056 public TimeSpan Duration 00057 { 00058 get { return new TimeSpan(this.row.Duration); } 00059 set { 00060 this.modified = true; 00061 this.row.Duration = value.Ticks; 00062 } 00063 } 00064 00068 public DateTime PerformedDate 00069 { 00070 get { return this.row.PerformedDate; } 00071 set { 00072 this.modified = true; 00073 this.row.PerformedDate = value; 00074 } 00075 } 00076 00080 public Person PerformedBy 00081 { 00082 get { 00083 return Person.GetById(this.row.UserId); 00084 } 00085 set { 00086 this.modified = true; 00087 this.row.UserId = value.Id; 00088 } 00089 } 00090 00094 public void Save(){ 00095 if(this.activity != null){ 00096 this.activity.Save(); 00097 if(this.row.SportsActivityId != this.activity.Id){ 00098 this.row.SportsActivityId = this.activity.Id; 00099 this.modified = true; 00100 } 00101 } 00102 if (this.row.ExerciseRowId == 0) 00103 Settings.Repo.Add(this.row); 00104 else{ 00105 if(this.modified) 00106 Settings.Repo.Update(this.row); 00107 } 00108 00109 this.modified = false; 00110 } 00111 00115 public void Delete() 00116 { 00117 if (this.row.ExerciseRowId != 0) 00118 Settings.Repo.Delete(this.row); 00119 this.row.ExerciseRowId = 0; 00120 this.modified = true; 00121 } 00122 00123 public static IEnumerable<Exercise> ListExercises(){ 00124 foreach(var exer in Settings.Repo.All<ExerciseRow>()) 00125 yield return new Exercise(exer); 00126 } 00127 00128 } 00129 }