public bool Create(PositionBindingModel model) { try { using (var libraryDb = new LibraryManagementEntities()) { // check duplicated var currentPosition = libraryDb.Positions.FirstOrDefault(s => s.Name == model.Name); if (currentPosition != null) { throw new ArgumentNullException(); } // Insert db var positionInfo = _entityMapper.Map <PositionBindingModel, Position>(model); libraryDb.Positions.Add(positionInfo); libraryDb.SaveChanges(); return(true); } } catch (Exception) { return(false); } }
public static Position ToModel(PositionBindingModel bindingModel) { if (bindingModel == null) { return(null); } var model = new Position(); model.PositionName = bindingModel.Name; model.PositionId = bindingModel.Id; return(model); }
public List <PositionViewModel> Read(PositionBindingModel model) { using (var context = new FurnitureFactoryDatabase()) { return(context.Positions.Where(rec => rec.Id == model.Id || model == null || rec.OrderId.Equals(model.OrderId)) .Select(rec => new PositionViewModel { Id = rec.Id, Count = rec.Count, FurnitureModelId = rec.FurnitureModelId, ModelName = rec.FurnitureModel.ModelName, Price = rec.Count * rec.FurnitureModel.Price, OrderId = rec.OrderId, TypeName = rec.FurnitureModel.TypeName }) .ToList()); } }
private void btnSave_Click(object sender, EventArgs e) { bool add = txtId.Text == "" ? false : true; if (!add) { PositionBindingModel model = new PositionBindingModel() { Name = txtName.Text }; if (manager.Create(model)) { add = false; MessageBox.Show("Thêm mới thành công !", "Thông báo"); } else { MessageBox.Show("Thêm mới không thành công!. Liên hệ với quản trị viên.", "Thông báo"); } } else { PositionBindingModel model = new PositionBindingModel() { Id = int.Parse(txtId.Text), Name = txtName.Text }; if (manager.Update(model)) { MessageBox.Show("Sửa thành công !", "Thông báo"); } else { MessageBox.Show("Sửa không thành công!. Liên hệ với quản trị viên.", "Thông báo"); } } LoadData(Searching, PageIndex, PageSize); }
public bool Update(PositionBindingModel model) { try { using (var libraryDb = new LibraryManagementEntities()) { // Check first get item var position = libraryDb.Positions.FirstOrDefault(s => s.Id == model.Id); if (position == null) { throw new ArgumentNullException("No exist"); } position.Name = model.Name; libraryDb.SaveChanges(); return(true); } } catch (Exception) { return(false); } }
public void CreateOrUpdate(PositionBindingModel model) { using (var context = new FurnitureFactoryDatabase()) { Position element = model.Id.HasValue ? null : new Position(); if (model.Id.HasValue) { element = context.Positions.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } } else { element = new Position(); context.Positions.Add(element); } element.Count = model.Count; element.FurnitureModelId = model.FurnitureModelId; element.OrderId = model.OrderId; context.SaveChanges(); } }
public async Task <PositionBindingModel> UpdatePosition([FromBody] PositionBindingModel updatePosition) { await _positionService.UpdateAsync(PositionAdapter.ToModel(updatePosition)); return(updatePosition); }
public async Task <PositionBindingModel> CreatePosition([FromBody] PositionBindingModel newPosition) { await _positionService.CreateAsync(PositionAdapter.ToModel(newPosition)); return(newPosition); }