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

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

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

        public void Edit(int id, FmsDrawingCodeGroup data) {

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

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

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

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

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

    }
}