private BhSpinOffSource MapDtoToModel(BhSpinOffDto dto, BhSpinOffSource model) { model.Id = dto.SourceId; ////model.BhTransactionId = dto.BhTransactionId; ////model.BhTransactionDate = dto.BhTransactionDate; model.SecurityId = dto.SecurityId; model.TickerSymbol = dto.TickerSymbol; model.Shares = dto.Shares; model.CostBasisSplit = dto.CostBasisSplit; ////Property(t => t.CostBasisSplit).HasColumnName("CostBasisSplit"); model.TransactionApplied = dto.SourceTransactionApplied; return model; }
// public BhSpinOffItem Create(BhSpinOffDto dto) public BhSpinOff Create(BhSpinOffDto dto) { // var model = new BhSpinOffItem var model = new BhSpinOff { Id = dto.Id, TransactionDate = dto.TransactionDate, Notes = dto.Notes, TransactionApplied = dto.TransactionApplied, }; var sourceModel = new BhSpinOffSource { Id = dto.SourceId, CostBasisSplit = dto.CostBasisSplit, SecurityId = dto.SecurityId, TickerSymbol = dto.TickerSymbol, Shares = dto.Shares, TransactionApplied = dto.SourceTransactionApplied }; model.BhSpinOffSources.Add(sourceModel); foreach (var linkDto in dto.SpinOffLinks) { var linkModel = new BhSpinOffLink { Url = linkDto.Url }; model.BhSpinOffLinks.Add(linkModel); } foreach (var itemDto in dto.SpinOffItems) { var itemModel = new BhSpinOffItem { BhTransactionDate = itemDto.BhTransactionDate, BhTransactionId = itemDto.BhTransactionId, TickerSymbol = itemDto.TickerSymbol, SecurityId = itemDto.SecurityId, Shares = itemDto.Shares, Ratio = itemDto.Ratio, CostBasisSplit = itemDto.CostBasisSplit, TransactionApplied = itemDto.TransactionApplied, }; model.BhSpinOffItems.Add(itemModel); } _context.BhSpinOffs.Add(model); try { _context.Database.Log = s => Debug.WriteLine(s); _context.SaveChanges(); } catch (DbEntityValidationException ex) { var w = ex.Message; } catch (Exception ex) { var w = ex.Message; } return model; }
public BhSpinOff Update(BhSpinOffDto dto) { //var model = _context.BhSpinOffItems.FirstOrDefault(p => p.Id == dto.Id); var model = _context.BhSpinOffs.FirstOrDefault(p => p.Id == dto.Id); model.TransactionDate = dto.TransactionDate; model.Notes = dto.Notes; model.TransactionApplied = dto.TransactionApplied; if (dto.SourceId > 0) { var sourcemodel = _context.BhSpinOffSources.FirstOrDefault(p => p.Id == dto.SourceId); MapDtoToModel(dto, sourcemodel); } else { model.BhSpinOffSources.Add(MapDtoToModel(dto, new BhSpinOffSource())); } var sourceModel = new BhSpinOffSource { Id = dto.SourceId, ////BhTransactionId = dto.BhTransactionId, ////BhTransactionDate = dto.BhTransactionDate, SecurityId = dto.SecurityId, TickerSymbol = dto.TickerSymbol, Shares = dto.Shares, TransactionApplied = dto.SourceTransactionApplied }; model.BhSpinOffSources.Add(sourceModel); foreach (var linkDto in dto.SpinOffLinks) { if (linkDto.Id > 0) { var linkModel = _context.BhSpinOffLinks.FirstOrDefault(p => p.Id == linkDto.Id); MapDtoToModel(linkDto, linkModel); } else { model.BhSpinOffLinks.Add(MapDtoToModel(linkDto, new BhSpinOffLink())); } } foreach (var itemDto in dto.SpinOffItems) { if (itemDto.Id > 0) { var itemModel = _context.BhSpinOffItems.FirstOrDefault(p => p.Id == itemDto.Id); MapDtoToModel(itemDto, itemModel); } else { model.BhSpinOffItems.Add(MapDtoToModel(itemDto, new BhSpinOffItem())); } } _context.BhSpinOffs.Add(model); _context.SaveChanges(); return model; }