예제 #1
0
        public StockmanDTO DeleteStockman(int id)
        {
            Stockman stockman = null;

            try
            {
                stockman = Database.Stockmen.Get(id);
                if (stockman.Details.Count == 0)
                {
                    var stock = Database.Stockmen.Delete(id);
                    // применяем автомаппер для проекции stockman на stockmanDTO
                    Mapper.Initialize(cfg => cfg.CreateMap <Stockman, StockmanDTO>());
                    return(Mapper.Map <Stockman, StockmanDTO>(stock));
                }
                else
                {
                    throw new Exception();
                }
            }
            catch (ValidationException ex)
            {
                ex = new ValidationException("Кладовщик не найден", "");
            }
            catch (Exception ex)
            {
                ex = new Exception("Кладовщик не может быть удален, так как на нем числятся детали!!!");
            }
            return(Mapper.Map <Stockman, StockmanDTO>(stockman));
        }
예제 #2
0
 public IActionResult Put(int id, [FromBody] Stockman stockman)
 {
     if (ModelState.IsValid)
     {
         db.Update(stockman);
         db.SaveChanges();
         return(Ok(stockman));
     }
     return(BadRequest(ModelState));
 }
예제 #3
0
 public IActionResult Post([FromBody] Stockman stockman)
 {
     if (ModelState.IsValid)
     {
         db.Stockmans.Add(stockman);
         db.SaveChanges();
         return(Ok(stockman));
     }
     return(BadRequest(ModelState));
 }
예제 #4
0
        public void InsertStockman(StockmanDTO StockmanDTO)
        {
            Stockman Stockman = new Stockman
            {
                StockmanId   = StockmanDTO.StockmanId,
                StockmanName = StockmanDTO.StockmanName,
            };

            Database.Stockmen.Create(Stockman);
            Database.Save();
        }
예제 #5
0
        public IActionResult Delete(int id)
        {
            Stockman stockman = db.Stockmans.FirstOrDefault(x => x.Id == id);

            if (stockman != null)
            {
                Detail detail = db.Details.Where(p => p.Quantity > 0).FirstOrDefault(x => x.StockmanId == stockman.Id);

                if (detail != null)
                {
                    return(BadRequest("Нельзя удалить кладовщика, за которым числятся детали"));
                }
                else
                {
                    db.Stockmans.Remove(stockman);
                    db.SaveChanges();
                }
            }
            return(Ok(stockman));
        }
예제 #6
0
        public void UpdateStockman(StockmanDTO StockmanDTO)
        {
            Mapper.Initialize(cfg => cfg.CreateMap <StockmanDTO, Stockman>());
            var newStockman = Mapper.Map <StockmanDTO, Stockman>(StockmanDTO);

            // Database.Stockmen.Update(newStockman);
            if (StockmanDTO.StockmanId == 0)
            {
                Database.Stockmen.Create(newStockman);
            }
            else
            {
                Stockman man = Database.Stockmen.Get(newStockman.StockmanId);
                if (man != null)
                {
                    man.StockmanId   = newStockman.StockmanId;
                    man.StockmanName = newStockman.StockmanName;
                    Database.Stockmen.Update(man);
                }
            }
        }
예제 #7
0
        public void InsertDetail(DetailDTO detailDTO)
        {
            Stockman stockman = Database.Stockmen.Get(detailDTO.StockmanId);

            // валидация
            if (stockman == null)
            {
                throw new ValidationException("Кладовщик не найден", "");
            }
            if (detailDTO.NomenclativeCode != null)
            {
                String input = @"^[a-zA-Z]{3}-\d{6}$";
                if (Regex.IsMatch(detailDTO.NomenclativeCode, input))
                {
                    Detail detail = new Detail
                    {
                        DetailId         = detailDTO.DetailId,
                        NomenclativeCode = detailDTO.NomenclativeCode,
                        DetailName       = detailDTO.DetailName,
                        SpecialConside   = detailDTO.SpecialConside,
                        DetailCount      = detailDTO.DetailCount,
                        DateAdded        = detailDTO.DateAdded,
                        StockmanId       = stockman.StockmanId,
                        Stockman         = stockman
                    };
                    Database.Details.Create(detail);
                    Database.Save();
                }
                else
                {
                    throw new ValidationException("Номенклатурный код не соответствует шаблону", "");
                }
            }
            else
            {
                throw new ValidationException("Заполните номенклатурный код", "");
            }
        }
예제 #8
0
        public Stockman Get(int id)
        {
            Stockman stockman = db.Stockmans.FirstOrDefault(x => x.Id == id);

            return(stockman);
        }