public DealDetailApiModel GetOne(int id) { var dbDeal = _dealRepository.GetOne(id); if (dbDeal != null) { var apiModel = new DealDetailApiModel(); apiModel.id = dbDeal.ID; apiModel.amount = dbDeal.Amount.GetValueOrDefault(); apiModel.closingDate = dbDeal.ClosingDate.GetValueOrDefault(); apiModel.expectedClosingDate = dbDeal.ExpectedClosingDate.GetValueOrDefault(); apiModel.CreatedAt = dbDeal.CreatedAt.GetValueOrDefault(); if (dbDeal.CreatedUser != null) { apiModel.CreatedBy = new UserLinkApiModel() { id = dbDeal.CreatedUser.ID, username = dbDeal.CreatedUser.Username, email = dbDeal.CreatedUser.Email }; } if (dbDeal.Owner != null) { apiModel.owner = new UserLinkApiModel() { id = dbDeal.Owner.ID, username = dbDeal.Owner.Username, email = dbDeal.Owner.Email }; } if (dbDeal.ACCOUNT != null) { apiModel.account = new AccountLinkApiModel() { id = dbDeal.ACCOUNT.ID, name = dbDeal.ACCOUNT.Name, email = dbDeal.ACCOUNT.Email }; } if (dbDeal.CONTACT != null) { apiModel.contact = new ContactLinkApiModel() { id = dbDeal.CONTACT.ID, name = dbDeal.CONTACT.Name, email = dbDeal.CONTACT.Email }; } if (dbDeal.PRIORITY != null) { apiModel.priorities = _priorityRepository.GetAllPriorities().Select(c => new PrioritySelectionApiModel() { id = c.ID, name = c.Name, selected = c.ID == dbDeal.PRIORITY.ID }).ToList(); } if (dbDeal.LOST_REASON != null) { apiModel.lostReason = new LostReasonLinkApiModel() { id = dbDeal.LOST_REASON.ID, reason = dbDeal.LOST_REASON.Reason }; } apiModel.expectedRevenue = dbDeal.ExpectedRevenue.GetValueOrDefault(); apiModel.name = dbDeal.Name; //apiModel. if (dbDeal.STAGE_HISTORY.Count > 0) { var histories = dbDeal.STAGE_HISTORY.OrderByDescending(sh => sh.ModifiedAt); var current = histories.First(); apiModel.probability = current.STAGE.Probability.GetValueOrDefault(); apiModel.stages = _dealRepository.GetAllStages().OrderBy(c => c.ID).Select(c => new StageLinkApiModel() { id = c.ID, name = c.Name, probability = c.Probability.GetValueOrDefault(), selected = c.ID == current.STAGE.ID, passed = c.ID < current.STAGE.ID }).ToList(); } if (dbDeal.CAMPAIGN != null) { apiModel.campaign = new CampaignLinkApiModel() { id = dbDeal.CAMPAIGN.ID, name = dbDeal.CAMPAIGN.Name }; } apiModel.CreatedAt = dbDeal.CreatedAt.GetValueOrDefault(); apiModel.CreatedBy = new UserLinkApiModel() { id = dbDeal.CreatedUser.ID, username = dbDeal.CreatedUser.Username, email = dbDeal.CreatedUser.Email }; apiModel.ModifiedAt = dbDeal.ModifiedAt.GetValueOrDefault(); apiModel.ModifiedBy = new UserLinkApiModel() { id = dbDeal.ModifiedUser?.ID, username = dbDeal.ModifiedUser?.Username, email = dbDeal.ModifiedUser?.Email }; apiModel.description = dbDeal.Description; apiModel.priorities = _priorityRepository.GetAllPriorities().Select(c => new PrioritySelectionApiModel() { id = c.ID, name = c.Name, selected = dbDeal.PRIORITY != null ? dbDeal.PRIORITY.ID == c.ID : false }).ToList(); //notes apiModel.notes = dbDeal.NOTEs.Select(c => new NoteApiModel() { id = c.ID, avatar = $"{StaticStrings.ServerHost}avatar?fileName={c.USER?.Avatar}", body = c.NoteBody, createdAt = c.CreatedAt.GetValueOrDefault(), createdBy = new UserLinkApiModel() { id = c.USER.ID, username = c.USER.Username, email = c.USER.Email }, files = c.FILEs.Select(f => new FileApiModel() { id = f.ID, fileName = f.FileName, size = f.FileSize.Value.ToString() + " KB", url = StaticStrings.ServerHost + "files/" + f.ID }).ToList() }).ToList(); //tags apiModel.tags = dbDeal.TAG_ITEM.Select(c => new TagApiModel() { id = c.TAG.ID, name = c.TAG.Name }).ToList(); return(apiModel); } else { return(null); } }