Example #1
0
        /// <summary>
        /// Add data to the relevant tables in the database.
        /// </summary>
        /// <param name="MovieName">The movie name</param>
        /// <param name="Genre">The genre</param>
        /// <param name="Actors">The actors</param>
        /// <param name="CreatedBy">User who created this data</param>
        /// <returns>Error message if adding data fails</returns>
        public string DataAdd(string MovieName, string Genre, List <string> Actors, string CreatedBy)
        {
            if (MovieName == null || Genre == null || Actors == null)
            {
                return("Movie, Genre and Actors fields are required.");
            }
            else if (CheckDuplicate(MovieName, -1) != null)
            {
                return(CheckDuplicate(MovieName, -1));
            }
            else
            {
                BingDBEntities db = new BingDBEntities();

                db.Movies.Add(new Movies {
                    MovieName = MovieName, CreatedBy = CreatedBy
                });
                db.SaveChanges();

                db.Genres.Add(new Genre {
                    GenreType = Genre, MovieID = MovieNameToID(MovieName)
                });
                foreach (string Actor in Actors)
                {
                    db.Actors.Add(new Actor {
                        MovieID = MovieNameToID(MovieName), ActorName = Actor
                    });
                }
                db.SaveChanges();

                return(null);
            }
        }
Example #2
0
        /// <summary>
        /// Delete a rule.
        /// </summary>
        /// <param name="question">The question</param>
        /// <param name="table">The specified Table</param>
        public void DeleteRule(string question, Table table)
        {
            using (var db = new BingDBEntities())
            {
                switch (table)
                {
                case Table.ApprovedRules:
                    var apprule = (from r in db.ApprovedRules
                                   where r.Question == question
                                   select r).First();
                    db.ApprovedRules.Remove(apprule);
                    break;

                case Table.RejectedRules:
                    var rejrule = (from r in db.RejectedRules
                                   where r.Question == question
                                   select r).First();
                    db.RejectedRules.Remove(rejrule);
                    break;

                case Table.PendingRules:
                    var penrule = (from r in db.PendingRules
                                   where r.Question == question
                                   select r).First();
                    db.PendingRules.Remove(penrule);
                    break;

                default:
                    System.Diagnostics.Debug.WriteLine("Unknown table");
                    return;
                }
                db.SaveChanges();
            }
        }
Example #3
0
 /// <summary>
 /// Reject a rule.
 /// </summary>
 /// <param name="question">The question</param>
 /// <param name="user">The current User</param>
 /// <param name="createdBy">The User who created the rule</param>
 /// <param name="lastEditedBy">The last User who edited the rule</param>
 public void RejectRule(string question, string user, string createdBy, string lastEditedBy)
 {
     using (var db = new BingDBEntities())
     {
         var penrule = (from r in db.PendingRules
                        where r.Question == question
                        select r).First();
         AddRule(penrule.Question, penrule.Answer, user, createdBy, lastEditedBy, penrule.Lookup, Table.RejectedRules);
         db.PendingRules.Remove(penrule);
         db.SaveChanges();
     }
 }
Example #4
0
        /// <summary>
        /// Handles data edits. Updates movie and genre details, deletes all existing actors and adds a new list of actors.
        /// </summary>
        /// <param name="MovieID">The movie ID</param>
        /// <param name="MovieName">The movie name</param>
        /// <param name="Genre">The movie genre</param>
        /// <param name="Actors">The actors of the movie</param>
        /// <param name="LastEditedBy">The last User who edits the data</param>
        /// <returns>Error message if editing fails</returns>
        public string EditData(int MovieID, string MovieName, string Genre, List <string> Actors, string LastEditedBy)
        {
            //ADD CHECK TO SEE IF MOVIE NAME ALREADY EXISTS
            if (MovieName == null || Genre == null || Actors == null)
            {
                return("Movie, Genre and Actors fields are required.");
            }
            else if (CheckDuplicate(MovieName, MovieID) != null)
            {
                return(CheckDuplicate(MovieName, MovieID));
            }


            using (var db = new BingDBEntities())
            {
                var MovieDB = db.Movies.Where(q => q.MovieID == MovieID).First();
                var GenreDB = db.Genres.Where(q => q.MovieID == MovieID).First();
                var ActorDB = db.Actors.Where(q => q.MovieID == MovieID).ToList();

                MovieDB.MovieName    = MovieName;
                MovieDB.LastEditedBy = LastEditedBy;
                GenreDB.GenreType    = Genre;

                foreach (Actor Actor in ActorDB)
                {
                    db.Actors.Remove(Actor);
                }
                db.SaveChanges();

                foreach (string Actor in Actors)
                {
                    db.Actors.Add(new Actor {
                        MovieID = MovieID, ActorName = Actor
                    });
                }
                db.SaveChanges();
            }

            return(null);
        }
Example #5
0
        /// <summary>
        /// Delete the data
        /// </summary>
        /// <param name="MovieID">The movie ID</param>
        public void DeleteData(int MovieID)
        {
            BingDBEntities db = new BingDBEntities();

            var MovieDB = db.Movies.Where(q => q.MovieID == MovieID).First();
            var GenreDB = db.Genres.Where(q => q.MovieID == MovieID).First();
            var ActorDB = db.Actors.Where(q => q.MovieID == MovieID).ToList();

            db.Movies.Remove(MovieDB);
            db.Genres.Remove(GenreDB);
            foreach (Actor Actor in ActorDB)
            {
                db.Actors.Remove(Actor);
            }
            db.SaveChanges();
        }
Example #6
0
        /// <summary>
        /// Edit an existing rule.
        /// </summary>
        /// <param name="id">The ID of the rule</param>
        /// <param name="question">The new question</param>
        /// <param name="answer">The new answer</param>
        /// <param name="user">The current User</param>
        /// <param name="Lookup">The lookup table for data driven rule</param>
        /// <param name="table">The specified Table</param>
        /// <returns>Error message if editing a rule fails</returns>
        public string EditRule(int id, string question, string answer, string user, string Lookup, Table table)
        {
            // Returns false if either question or response is empty
            if (question == null || answer == null)
            {
                return("Question and Answer fields are required.");
            }

            // If it is data driven, make sure it is in the right format
            if (Lookup != null && !((answer == "{Movies}" || answer == "{Genres}" || answer == "{Actors}") && (question.Contains("{%}"))))
            {
                return("You are attempting to make a data driven rule. Ensure your question contains {%} and your answer is either {Movies}, {Genres} or {Actors}.");
            }

            using (var db = new BingDBEntities())
            {
                question = question.TrimEnd(' ');

                if (CheckExisting(question, id))
                {
                    return("This question already exists, please use another.");
                }

                switch (table)
                {
                case Table.ApprovedRules:
                    var apprule = (from r in db.ApprovedRules
                                   where r.RuleID == id
                                   select r).First();
                    apprule.Question     = question;
                    apprule.Answer       = answer;
                    apprule.LastEditedBy = user;
                    apprule.Lookup       = Lookup;
                    break;

                case Table.RejectedRules:
                    var rejrule = (from r in db.RejectedRules
                                   where r.RuleID == id
                                   select r).First();
                    rejrule.Question     = question;
                    rejrule.Answer       = answer;
                    rejrule.LastEditedBy = user;
                    rejrule.Lookup       = Lookup;
                    break;

                case Table.PendingRules:
                    var penrule = (from r in db.PendingRules
                                   where r.RuleID == id
                                   select r).First();
                    penrule.Question     = question;
                    penrule.Answer       = answer;
                    penrule.LastEditedBy = user;
                    penrule.Lookup       = Lookup;
                    break;

                default:
                    System.Diagnostics.Debug.WriteLine("Unknown table");
                    return("Sorry something went wrong. Try reload the page and try again.");
                }
                db.SaveChanges();
                return(null);
            }
        }
Example #7
0
        /// <summary>
        /// Add a new rule into the specified Table.
        /// </summary>
        /// <param name="question">The question</param>
        /// <param name="response">The answer to the question</param>
        /// <param name="user">The current User</param>
        /// <param name="createdBy">The User who created the rule</param>
        /// <param name="lastEditedBy">The last User who edited the rule</param>
        /// <param name="Lookup">The lookup table for data driven rule</param>
        /// <param name="table">The specified Table</param>
        /// <returns>Error message if adding a rule fails</returns>
        public string AddRule(string question, string response, string user, string createdBy, string lastEditedBy, string Lookup, Table table)
        {
            // Returns false if either question or response is empty
            if (question == null || response == null)
            {
                return("Question and Answer fields are required.");
            }
            // If it is data driven, make sure it is in the right format.
            if (Lookup != null && !((response == "{Movies}" || response == "{Genres}" || response == "{Actors}") && (question.Contains("{%}"))))
            {
                return("You are attempting to make a data driven rule. Ensure your question contains {%} and your answer is either {Movies}, {Genres} or {Actors}.");
            }

            // Remove extra whitespace from the question
            question = Regex.Replace(question, "\\s+", " ").Trim();
            using (var db = new BingDBEntities())
            {
                question = question.TrimEnd(' ');

                switch (table)
                {
                case Table.ApprovedRules:
                    var apprule = new ApprovedRule
                    {
                        Question     = question,
                        Answer       = response,
                        ApprovedBy   = user,
                        LastEditedBy = lastEditedBy,
                        CreatedBy    = createdBy,
                        Lookup       = Lookup
                    };
                    db.ApprovedRules.Add(apprule);
                    break;

                case Table.RejectedRules:
                    var rejrule = new RejectedRule
                    {
                        Question     = question,
                        Answer       = response,
                        RejectedBy   = user,
                        LastEditedBy = lastEditedBy,
                        CreatedBy    = createdBy,
                        Lookup       = Lookup
                    };
                    db.RejectedRules.Add(rejrule);
                    break;

                case Table.PendingRules:
                    if (CheckExisting(question, -1))
                    {
                        return("This question already exists, please use another.");
                    }
                    var penrule = new PendingRule
                    {
                        Question     = question,
                        Answer       = response,
                        LastEditedBy = user,
                        CreatedBy    = user,
                        Lookup       = Lookup
                    };
                    db.PendingRules.Add(penrule);
                    break;

                default:
                    System.Diagnostics.Debug.WriteLine("Unknown table");
                    return("Sorry something went wrong. Try reload the page and try again.");
                }
                db.SaveChanges();
                return(null);
            }
        }