public OpportunityTableType InsertOpportunity(OpportunityTableType opportunity) { IEnumerable <Image> images = new List <Image>(); IEnumerable <OpportunityTableType> opportunities = new List <OpportunityTableType>() { opportunity }; if (opportunity.OpportunityImage != null) { images = new List <Image>() { opportunity.OpportunityImage } } ; using (var db = ObjectContextFactory.Create()) { db.QueryStoredProc("dbo.SaveOpportunity", (reader) => { opportunity.OpportunityID = reader.Read <int>().FirstOrDefault(); }, new { opportunity = opportunities.AsTableValuedParameter("dbo.OpportunityTableType", new string[] { "OpportunityID", "OpportunityName", "Potential", "StageID", "ExpectedClosingDate", "Description", "Owner", "AccountID", "CreatedBy", "CreatedOn", "LastModifiedBy", "LastModifiedOn", "IsDeleted", "OpportunityType", "ProductType", "Address", "ImageID" }), image = images.AsTableValuedParameter("dbo.ImageType", new string[] { "ImageID", "FriendlyName", "StorageName", "OriginalName", "CreatedBy", "CreatedDate", "CategoryId", "AccountID" }), }); return(opportunity); } }
public UpdateOpportunityResponse UpdateOpportunity(UpdateOpportunityRequest request) { Logger.Current.Verbose("Request for updating an opportunity"); request.opportunityViewModel.IsDeleted = false; var opportunityViewModel = request.opportunityViewModel; hasAccess(request.opportunityViewModel.OpportunityID, request.RequestedBy, request.AccountId, request.RoleId); if (request.opportunityViewModel.Image != null) { SaveImageResponse imageResponse = imageService.SaveImage(new SaveImageRequest() { ImageCategory = ImageCategory.OpportunityProfile, ViewModel = request.opportunityViewModel.Image, AccountId = request.AccountId }); request.opportunityViewModel.Image = imageResponse.ImageViewModel; } Opportunity opportunity = Mapper.Map <OpportunityViewModel, Opportunity>(request.opportunityViewModel); bool isOpportunityUnique = opportunityRepository.IsOpportunityUnique(opportunity); if (!isOpportunityUnique) { var message = "[|Opportunity with name|] \"" + opportunity.OpportunityName + "\" [|already exists.|]"; throw new InvalidOperationException(message); } isOpportunityValid(opportunity); //opportunityRepository.Update(opportunity); // Opportunity updatedOpportunity = unitOfWork.Commit() as Opportunity; OpportunityTableType opportunityTableType = Mapper.Map <OpportunityViewModel, OpportunityTableType>(request.opportunityViewModel); OpportunityTableType newOpportunityType = opportunityRepository.InsertOpportunity(opportunityTableType); // opportunityRepository.Insert(opportunity); Opportunity updatedOpportunity = opportunityRepository.FindBy(newOpportunityType.OpportunityID); if (indexingService.Update <Opportunity>(updatedOpportunity) > 0) { Logger.Current.Verbose("Opportunity updated to elasticsearch successfully"); } if (request.opportunityViewModel.StageID != request.opportunityViewModel.PreviousStageID) { addToTopic(opportunityViewModel.OpportunityID, request.AccountId, opportunityViewModel.Contacts.Select(s => s.Id), opportunityViewModel.StageID); } return(new UpdateOpportunityResponse()); }
/// <summary> /// Insert Opportunity. /// </summary> /// <param name="request"></param> /// <returns>HttpResponseMessage</returns> public InsertOpportunityResponse InsertOpportunity(InsertOpportunityRequest request) { Logger.Current.Verbose("Request for inserting an opportunity"); if (request.opportunityViewModel.Image != null) { SaveImageResponse imageResponse = imageService.SaveImage(new SaveImageRequest() { ImageCategory = ImageCategory.OpportunityProfile, ViewModel = request.opportunityViewModel.Image, AccountId = request.AccountId }); request.opportunityViewModel.Image = imageResponse.ImageViewModel; } Opportunity opportunity = Mapper.Map <OpportunityViewModel, Opportunity>(request.opportunityViewModel); opportunity.IsDeleted = false; bool isOpportunityUnique = opportunityRepository.IsOpportunityUnique(opportunity); if (!isOpportunityUnique) { var message = "[|Opportunity with name|] \"" + opportunity.OpportunityName + "\" [|already exists.|]"; throw new UnsupportedOperationException(message); } isOpportunityValid(opportunity); OpportunityTableType opportunityTableType = Mapper.Map <OpportunityViewModel, OpportunityTableType>(request.opportunityViewModel); OpportunityTableType newOpportunityType = opportunityRepository.InsertOpportunity(opportunityTableType); // opportunityRepository.Insert(opportunity); Opportunity newOpportunity = opportunityRepository.FindBy(newOpportunityType.OpportunityID); if (indexingService.Index <Opportunity>(newOpportunity) > 0) { Logger.Current.Verbose("Indexed the opportunity successfully"); } return(new InsertOpportunityResponse()); }