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


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


        public Task <List<Grade>> GetGradeAsync(string strCurrentUser)
        {
            List<Grade> ColGrade = new List<Grade>();
            ColGrade = (from grade in _context.Grade select grade).ToList();
            return Task.FromResult(ColGrade);
        }

        public Task<Grade> CreateGradeAsync(Grade objGrade)
        {
            _context.Grade.Add(objGrade);
            _context.SaveChanges();
            return Task.FromResult(objGrade);
        }


        public Task<bool> UpdateGradeAsync(Grade objGrade)
        {
            var ExistingGrade =
                _context.Grade
                .Where(x => x.GradeID == objGrade.GradeID)
                .FirstOrDefault();
            if (ExistingGrade != null)
            {
                ExistingGrade.value =
                    objGrade.value;
                ExistingGrade.Comment =
                    objGrade.Comment;
                ExistingGrade.State =
                    objGrade.State;
                ExistingGrade.StudentID =
                    objGrade.StudentID;
                ExistingGrade.JudgeID =
                   objGrade.JudgeID;
                ExistingGrade.QuestionID =
                   objGrade.QuestionID;

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



    }
}
