//Purpose: Gets a record from the 'Activity' table whose row id is 'id' //Input: 'id' of the required record //Output: a record from 'Activity' table whose key is 'id' // If any exception occurs, the exception message is printed and the exception is thrown public ActivityGroupDetails Get(int id) { try { //Activity act = db.Activities.Find(id); ActivityGroupDetails agd = new ActivityGroupDetails(); Activity act = db.Activities.Find(id); agd.heading = act.heading; agd.description = act.description; agd.type = act.type; agd.category = act.category; agd.Questions = act.Questions; agd.Answers = act.Answers; return(agd); // return db.Activities.Find(id); //return db.Activities.Include(a => a); } catch (Exception e) { Console.WriteLine("Error occured: " + e.Message); throw e; } }
//Purpose: Adds an object 'activity' in the 'Activity' table //Input: 'activity' object of type 'Activity.cs' //Output: Returns the object 'activity' upon its successful addition in the table // If any exception occurs, the exception message is printed and the exception is thrown public Activity Add(ActivityGroupDetails act) { if (act == null) { throw new ArgumentNullException("activity"); } try { LinkedList <Activity_Group> grps = new LinkedList <Activity_Group>(); //Activity_Group ag = new Activity_Group(); if (act.Groups != null) { foreach (Group g in act.Groups) { Activity_Group ag = new Activity_Group(); ag.group_id = (db.Groups.Where(group => group.name == g.name).FirstOrDefault()).id; grps.AddLast(ag); //grps.Add(ag); } } Activity activity = new Activity(); activity.heading = act.heading; activity.description = act.description; activity.type = act.type; activity.category = act.category; activity.createdby = act.createdby; activity.Questions = act.Questions; activity.Answers = act.Answers; activity.Activity_Group = grps; db.Activities.Add(activity); db.SaveChanges(); return(activity); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } catch (Exception e) { Console.WriteLine("Error occured: " + e.Message); throw e; } }
//Purpose: Invokes 'Add(Activity activity) method of 'PollRepository.cs' that adds an object 'activity' in the 'Activity' table //Input: 'activity' object of type 'Activity.cs' //Output: Returns the object 'activity' upon its successful addition in the table public Activity PostPoll(ActivityGroupDetails activity) { try { return(repo.Add(activity)); }catch (Exception ex) { throw ex; } }
//Purpose: Invokes 'Add(Activity activity) method of 'PollRepository.cs' that adds an object 'activity' in the 'Activity' table //Input: 'activity' object of type 'Activity.cs' //Output: Returns the object 'activity' upon its successful addition in the table public Activity PostPoll(ActivityGroupDetails activity) { try { //Activity act = new Activity(); // Console.WriteLine(activity); //return act; return(repo.Add(activity)); } catch (Exception ex) { throw ex; } }