Example #1
0
        /// <summary>
        /// Method used to update
        /// </summary>
        /// <param name="course">The courseProfessor object that the user is adding/updating</param>
        /// <param name="contextCourse">The EF model for the course that will be saved to the database.</param>
        private void AddUpdateProfessor(CourseProfessorDto course, Course contextCourse)
        {
            // If we don't have a professor, don't worry about doing anything with the professor.
            if (course.ProfessorName != null && !course.ProfessorName.Equals(string.Empty))
            {
                // Do we already have this professor?
                var professorToUse = _context.Professor.Where(p => p.ProfessorName.ToUpper() == course.ProfessorName.ToUpper()).FirstOrDefault();
                if (professorToUse != null)
                {
                    // If the professor already eixsts, use the id when creating the course
                    contextCourse.ProfessorId = professorToUse.Id;

                    // If the professor's email changed, update it as well
                    if ((string.IsNullOrEmpty(professorToUse.Email) != string.IsNullOrEmpty(course.ProfessorEmail)) ||
                        !professorToUse.Email.ToUpper().Equals(course.ProfessorEmail.ToUpper()))
                    {
                        // Prepare the context to be updated
                        professorToUse.Email = course.ProfessorEmail;
                        _context.Update(professorToUse);
                        // Save the course changes to the database
                        _context.SaveChanges();
                    }
                }
                else
                {
                    // If we don't have the professor, we will need to add it to the database.
                    var professorToAdd = _mapper.Map <Professor>(course);
                    _context.Professor.Add(professorToAdd);
                    _context.SaveChanges();
                    contextCourse.ProfessorId = professorToAdd.Id;
                }
            }
        }
Example #2
0
        public async Task <IActionResult> UpdateCourse(CourseProfessorDto course)
        {
            try
            {
                // Clean the input strings
                CleanInput(course);

                // See if we can find the course in the database
                var origCourse = _context.Course.Find(course.ID);

                // If not found return an error
                if (origCourse == null)
                {
                    return(NotFound());
                }

                // Prep the course object
                var courseToUpdate = _mapper.Map <Course>(course);

                AddUpdateProfessor(course, courseToUpdate);

                // Prepare the context to be updated
                _context.Entry(origCourse).CurrentValues.SetValues(courseToUpdate);

                // Save the course changes to the database
                await _context.SaveChangesAsync();

                return(Ok());
            }
            catch
            {
                return(BadRequest("Failed to update the course"));
            }
        }
Example #3
0
        public async Task <IActionResult> AddCourse(CourseProfessorDto course)
        {
            try
            {
                // Clean the input strings
                CleanInput(course);

                // Check if the course already exists in the database
                if (_context.Course.Where(c => c.CourseName.Trim().ToUpper() == course.CourseName.Trim().ToUpper()).Any())
                {
                    // Return an error if we already found a course with the same name
                    return(Conflict("Course already exists"));
                }

                // Prep the course object
                var courseToAdd = _mapper.Map <Course>(course);

                AddUpdateProfessor(course, courseToAdd);

                // Add the new course
                _context.Add(courseToAdd);

                // Save the changes to the database
                await _context.SaveChangesAsync();

                return(Ok());
            }
            catch
            {
                return(BadRequest("Failed to add the course"));
            }
        }
Example #4
0
 /// <summary>
 /// Method used to scrub the string and remove trailing spaces
 /// </summary>
 /// <param name="course"></param>
 private void CleanInput(CourseProfessorDto course)
 {
     course.CourseName     = course.CourseName.Trim();
     course.ProfessorEmail = course.ProfessorEmail == null ? null : course.ProfessorEmail.Trim();
     course.ProfessorName  = course.ProfessorName == null ? null : course.ProfessorName.Trim();
 }