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

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

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

        public void Edit(int id, CmInvestigation2CodeGroup data) {

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

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

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

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

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

    }
}