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

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

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

        public void Edit(int id, FmsDrawingCodeType data) {

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

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

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

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

    }
}