using System.Linq;
using FMSAdmin.Data;
using FMSAdmin.Entities;
using Microsoft.Extensions.Logging;

namespace FMSAdmin.Services {
    public class Investigation1CodeGroupService {
        private readonly ILogger<Investigation1CodeGroupService> _logger;
        private readonly FMSContext _context;

        public Investigation1CodeGroupService(
            ILogger<Investigation1CodeGroupService> logger,
            FMSContext context) {
            _logger = logger;
            _context = context;
        }
        public void Create(CmInvestigation1CodeGroup data) {
            _context.CmInvestigation1CodeGroup.Add(data);
            _context.SaveChanges();
        }

        public void Edit(int id, CmInvestigation1CodeGroup data) {

            var persist = _context.CmInvestigation1CodeGroup
                            .Where(x => x.InvestigationGroupId == id).FirstOrDefault();
            if (persist == null) {
                throw new ServiceException("정보를 찾을 수 없습니다.");
            }

            persist.Name = data.Name;
            persist.IsUse = data.IsUse;

            _context.CmInvestigation1CodeGroup.Update(persist);
            _context.SaveChanges();
        }
        public void Delete(int id) {
            var data = _context.CmInvestigation1CodeGroup.First(x => x.InvestigationGroupId == id);
            _context.CmInvestigation1CodeGroup.Remove(data);
            _context.SaveChanges();
        }

        public IQueryable<CmInvestigation1CodeGroup> GetAll() {
            var query = _context.CmInvestigation1CodeGroup;
            return query;
        }

        public IQueryable<CmInvestigation1CodeGroup> Get(int id) {
            var data = _context.CmInvestigation1CodeGroup.Where(x => x.InvestigationGroupId == id);
            return data;
        }

    }
}