public static int UpdateAssignment <T>(int id, AssignmentAttributes attribute, T newValue)
        {
            using (SchoolContext sc = new SchoolContext())
            {
                Assignment assignment;

                try
                {
                    assignment = sc.Assignments.Find(id);
                    switch (attribute)
                    {
                    case AssignmentAttributes.Title:
                        assignment.Title = newValue.ToString();
                        break;

                    case AssignmentAttributes.Description:
                        assignment.Description = newValue.ToString();
                        break;

                    case AssignmentAttributes.SubmissionDate:
                        assignment.SubmissionDate = (DateTime)(object)newValue;
                        break;

                    case AssignmentAttributes.OralMark:
                        assignment.OralMark = (Decimal)(object)newValue;
                        break;

                    case AssignmentAttributes.TotalMark:
                        assignment.TotalMark = (Decimal)(object)newValue;
                        break;

                    default:
                        break;
                    }
                    return(sc.SaveChanges());
                }
                catch (Exception)
                {
                    return(0);
                }
            }
        }
예제 #2
0
        public void Update()
        {
            bool exit;

            ICollection <Assignment> assignments = DBAssignment.ReadAssignments();

            if (assignments.Count() == 0)
            {
                ConsoleUI.ShowLine("No assignments yet");
                ConsoleUI.ReadKey();
                ConsoleUI.Clear();
                return;
            }
            else
            {
                ConsoleUI.ShowLine("select assignment to update, type 0 to exit");
                foreach (Assignment s in assignments)
                {
                    ConsoleUI.ShowLine(s);
                }
            }


            exit = ConsoleUI.GetInt(out int AssignmentID, "give assignment id: ");
            if (exit)
            {
                return;
            }

            ConsoleUI.Clear();

            Assignment assignment;

            try
            {
                assignment = assignments.Where(a => a.Id == AssignmentID).First();
            }
            catch (Exception)
            {
                ConsoleUI.ShowLine($"NO ASSIGNMENT FOUND WITH ID: {AssignmentID}");
                ConsoleUI.ReadKey();
                return;
            }

            ConsoleUI.ShowLine($"you selected to edit assignment: {assignment.Title}");

            ConsoleUI.ShowLine($"select attribute to edit, type 0 anytime to exit");
            ConsoleUI.ShowLine("1. Title");
            ConsoleUI.ShowLine("2. Description");
            ConsoleUI.ShowLine("3. Date of submission");
            ConsoleUI.ShowLine("4. Oral Mark");
            ConsoleUI.ShowLine("5. Total Mark");

            exit = ConsoleUI.GetInt(out int choice);
            if (exit)
            {
                return;
            }
            ConsoleUI.Clear();

            AssignmentAttributes attribute = (AssignmentAttributes)choice;
            string   newInput = "";
            DateTime?newDate;
            decimal  newMark;

            int result = 0;

            switch (attribute)
            {
            case AssignmentAttributes.Title:
                exit = ConsoleUI.GetString(out newInput, "enter new title: ");
                if (exit)
                {
                    return;
                }

                result = DBAssignment.UpdateAssignment(AssignmentID, attribute, newInput);

                break;

            case AssignmentAttributes.Description:
                exit = ConsoleUI.GetString(out newInput, "enter new description: ");
                if (exit)
                {
                    return;
                }

                result = DBAssignment.UpdateAssignment(AssignmentID, attribute, newInput);

                break;

            case AssignmentAttributes.SubmissionDate:
                exit = ConsoleUI.GetDate(out newDate, "enter new date of submission: ");
                if (exit)
                {
                    return;
                }

                result = DBAssignment.UpdateAssignment(AssignmentID, attribute, newDate);

                break;

            case AssignmentAttributes.OralMark:
                exit = ConsoleUI.GetDecimal(out newMark, "enter new oral mark: ");
                if (exit)
                {
                    return;
                }

                result = DBAssignment.UpdateAssignment(AssignmentID, attribute, newMark);

                break;

            case AssignmentAttributes.TotalMark:
                exit = ConsoleUI.GetDecimal(out newMark, "enter new total mark: ");
                if (exit)
                {
                    return;
                }

                result = DBAssignment.UpdateAssignment(AssignmentID, attribute, newMark);

                break;

            default:
                break;
            }

            if (result == 0)
            {
                ConsoleUI.ShowLine("assignment update failed");
            }
            else
            {
                ConsoleUI.ShowLine("assignment updated successfully");
            }
            ConsoleUI.ReadKey();
        }