using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using SPWA.DataAccess.Data.MJDbContext;
using SPWA.DataAccess.Data.Models;

namespace SPWA.DataAccess.Data.CourseService
{
    public class CourseService
    {


        private readonly MJContext _context;
        public CourseService(MJContext context)
        {
            _context = context;
        }


        public Task <List<Course>> GetCourseAsync(string strCurrentUser)
        {
            List<Course> ColCourse = new List<Course>();
            ColCourse = (from course in _context.Course select course).ToList();
            return Task.FromResult(ColCourse);
        }

        public Task<Course> CreateCourseAsync(Course objCourse)
        {
            _context.Course.Add(objCourse);
            _context.SaveChanges();
            return Task.FromResult(objCourse);
        }


        public Task<bool> UpdateCourseAsync(Course objCourse)
        {
            var ExistingCourse =
                _context.Course
                .Where(x => x.CourseID == objCourse.CourseID)
                .FirstOrDefault();
            if (ExistingCourse != null)
            {
                ExistingCourse.CourseName =
                    objCourse.CourseName;
                ExistingCourse.CourseID =
                    objCourse.CourseID;
                ExistingCourse.Active =
                    objCourse.Active;
                //ExistingCourse.TermId =
                  //  objCourse.TermId;

                _context.SaveChanges();
            }
            else
            {
                return Task.FromResult(false);
            }
            return Task.FromResult(true);
        }



    }
}
