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

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

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

        public void Edit(int id, CmInvestigation2CodeType data) {

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

            persist.Name = data.Name;
            persist.IsUse = data.IsUse;
            _context.CmInvestigation2CodeType.Update(persist);
            _context.SaveChanges();
        }
        public void Delete(int id) {
            var data = _context.CmInvestigation2CodeType.First(x => x.InvestigationTypeId == id);
            _context.CmInvestigation2CodeType.Remove(data);
            _context.SaveChanges();
        }

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

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

    }
}