using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ViewModels;

namespace EntityDAL.Respostories
{
    public interface IRoleRepository
    {
        IList<RoleViewModel> GetRoles();
        void UpdateRole(RoleViewModel roleViewModel);
    }

    public class Repository : IRoleRepository
    {
        private readonly TempDataBaseEntities2 _context;

        public Repository()
        {
            _context = new TempDataBaseEntities2();
        }

        public static RoleViewModel Map(Role role)
        {
            return new RoleViewModel
            {
                Id = role.Id,
                Name = role.Name,
                IsInRole = role.IsInRole
            };
        }

        #region Implementation of IRoleRepository

        public IList<RoleViewModel> GetRoles()
        {
            return _context.Roles.ToList().Select(Map).ToList();
        }

        public void UpdateRole(RoleViewModel roleViewModel)
        {
            var role = _context.Roles.FirstOrDefault(r => r.Id == roleViewModel.Id);

            if (role == null) return;

            role.IsInRole = roleViewModel.IsInRole;
            _context.SaveChanges();
        }

        #endregion
    }
}
