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


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


        public Task <List<Template>> GetTemplateAsync(string strCurrentUser)
        {
            List<Template> ColTemplate = new List<Template>();
            ColTemplate = (from template in _context.Template select template).ToList();
            return Task.FromResult(ColTemplate);
        }

        public Task<Template> CreateTemplateAsync(Template objTemplate)
        {
            _context.Template.Add(objTemplate);
            _context.SaveChanges();
            return Task.FromResult(objTemplate);
        }


        public Task<bool> UpdateTemplateAsync(Template objTemplate)
        {
            var ExistingTemplate =
                _context.Template
                .Where(x => x.TemplateID == objTemplate.TemplateID)
                .FirstOrDefault();
            if (ExistingTemplate != null)
            {
                ExistingTemplate.Name =
                    objTemplate.Name;
                ExistingTemplate.Subject =
                    objTemplate.Subject;
                ExistingTemplate.Body =
                    objTemplate.Body;
               

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



    }
}
