public SaveResult InsertOrUpdate(ref ShopItemDTO item) { try { using (var context = DataAccessHelper.CreateContext()) { long ShopItemId = item.ShopItemId; ShopItem entity = context.shopitem.SingleOrDefault(c => c.ShopItemId.Equals(ShopItemId)); if (entity == null) //new entity { item = Insert(item, context); return SaveResult.Inserted; } else //existing entity { item = Update(entity, item, context); return SaveResult.Updated; } } } catch (Exception e) { Logger.Log.ErrorFormat(Language.Instance.GetMessageFromKey("UPDATE_ACCOUNT_ERROR"), item.ShopItemId, e.Message); return SaveResult.Error; } }
private ShopItemDTO Insert(ShopItemDTO shopitem, OpenNosContainer context) { ShopItem entity = Mapper.Map<ShopItem>(shopitem); context.shopitem.Add(entity); context.SaveChanges(); return Mapper.Map<ShopItemDTO>(entity); }
private ShopItemDTO Update(ShopItem entity, ShopItemDTO shopitem, OpenNosContainer context) { using (context) { var result = context.shopitem.SingleOrDefault(c => c.ShopItemId.Equals(shopitem.ShopItemId)); if (result != null) { result = Mapper.Map<ShopItemDTO, ShopItem>(shopitem, entity); context.SaveChanges(); } } return Mapper.Map<ShopItemDTO>(entity); }
public void ImportShopItems() { List<ShopItemDTO> shopitems = new List<ShopItemDTO>(); int itemCounter = 0; byte type = 0; foreach (string[] currentPacket in _packetList.Where(o => o[0].Equals("n_inv") || o[0].Equals("shopping"))) { if (currentPacket[0].Equals("n_inv")) { if (DAOFactory.ShopDAO.LoadByNpc(short.Parse(currentPacket[2])) != null) { for (int i = 5; i < currentPacket.Length; i++) { string[] item = currentPacket[i].Split('.'); ShopItemDTO sitem = null; if (item.Length == 5) { sitem = new ShopItemDTO { ShopId = DAOFactory.ShopDAO.LoadByNpc(short.Parse(currentPacket[2])).ShopId, Type = type, Slot = byte.Parse(item[1]), ItemVNum = short.Parse(item[2]) }; } else if (item.Length == 6) { sitem = new ShopItemDTO { ShopId = DAOFactory.ShopDAO.LoadByNpc(short.Parse(currentPacket[2])).ShopId, Type = type, Slot = byte.Parse(item[1]), ItemVNum = short.Parse(item[2]), Rare = sbyte.Parse(item[3]), Upgrade = byte.Parse(item[4]) }; } if (sitem == null || shopitems.Any(s => s.ItemVNum.Equals(sitem.ItemVNum) && s.ShopId.Equals(sitem.ShopId)) || DAOFactory.ShopItemDAO.LoadByShopId(sitem.ShopId).Any(s => s.ItemVNum.Equals(sitem.ItemVNum))) { continue; } shopitems.Add(sitem); itemCounter++; } } } else { if (currentPacket.Length > 3) { type = byte.Parse(currentPacket[1]); } } } DAOFactory.ShopItemDAO.Insert(shopitems); Logger.Log.Info(String.Format(Language.Instance.GetMessageFromKey("SHOPITEMS_PARSED"), itemCounter)); }