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

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

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

        public void Edit(int id, CmInvestigation3CodeGroup data) {

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

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

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

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

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

    }
}