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.TermServices
{
    public class TermService
    {


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


        public Task <List<Term>> GetTermAsync(string strCurrentUser)
        {
            List<Term> ColTerm = new List<Term>();
            ColTerm = (from term in _context.Term select term).ToList();
            return Task.FromResult(ColTerm);
        }

        public Task<Term> CreateTermAsync(Term objTerm)
        {
            _context.Term.Add(objTerm);
            _context.SaveChanges();
            return Task.FromResult(objTerm);
        }


        public Task<bool> UpdateTermAsync(Term objTerm)
        {
            var ExistingTerm =
                _context.Term
                .Where(x => x.TermID == objTerm.TermID)
                .FirstOrDefault();
            if (ExistingTerm != null)
            {
                ExistingTerm.Name =
                    objTerm.Name;
                ExistingTerm.StudentPerJudge =
                    objTerm.StudentPerJudge;
                ExistingTerm.Location =
                    objTerm.Location;
                ExistingTerm.MapUrl =
                    objTerm.MapUrl;

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



    }
}
