public OperationResult EmailVote(EmailVote vote) { OperationResult result; var emailVoteRequest = this.petitionVoteRepository.CreateEmailVoteRequest(vote); if (!emailVoteRequest.IsSuccess) { result = emailVoteRequest; return result; } var notification = new PetitionVoteNotification(emailVoteRequest.Data); var sendingResult = NotificationService.Send(notification); if (sendingResult.IsSuccess) { result = sendingResult; } else { result = OperationResult.Fail( -11, string.Format("Не вдалось відправити запит на підтвердження голосування на email {0}", vote.Email)); } return result; }
public OperationResult<Model.BusinessEntities.PetitionEmailVote> CreateEmailVoteRequest(EmailVote vote) { OperationResult<Model.BusinessEntities.PetitionEmailVote> emailVoteRequestResult; Func<EDEntities, OperationResult<Model.BusinessEntities.PetitionEmailVote>> procedure = (db) => { OperationResult<Model.BusinessEntities.PetitionEmailVote> result; var emailVote = db.PetitionEmailVotes.SingleOrDefault(v => v.PetitionID == vote.ID && v.Email == vote.Email); if (emailVote != null) { var votedPetition = db.Petitions.SingleOrDefault(p => p.ID == vote.ID); result = emailVote.IsConfirmed ? OperationResult<Model.BusinessEntities.PetitionEmailVote>.Fail( int.Parse(PetitionVoteOperationResult.AlreadyVotedCode), PetitionVoteOperationResult.AlreadyVotedMessage) : OperationResult<Model.BusinessEntities.PetitionEmailVote>.Success( int.Parse(PetitionVoteOperationResult.WaitingConfirmationCode), string.Format(PetitionVoteOperationResult.WaitingConfirmationMessage, emailVote.Email), new Model.BusinessEntities.PetitionEmailVote(emailVote, votedPetition)); return result; } emailVote = new PetitionEmailVote { PetitionID = vote.ID, Email = vote.Email, CreatedDate = DateTime.Now, IsConfirmed = false, Hash = HashGenerator.Generate() }; db.PetitionEmailVotes.Add(emailVote); db.SaveChanges(); var petition = new Model.BusinessEntities.Petition(db.Petitions.SingleOrDefault(p => p.ID == emailVote.PetitionID)); var clientEmailVote = new Model.BusinessEntities.PetitionEmailVote() { ID = emailVote.ID, Petition = petition, Hash = emailVote.Hash, Email = emailVote.Email, CreatedDate = emailVote.CreatedDate, IsConfirmed = emailVote.IsConfirmed }; result = OperationResult<Model.BusinessEntities.PetitionEmailVote>.Success( int.Parse(PetitionVoteOperationResult.EmailVoteRequestCreatedCode), string.Format(PetitionVoteOperationResult.EmailVoteRequestCreatedMessage, emailVote.Email), clientEmailVote); return result; }; emailVoteRequestResult = DbExecuter.Execute(procedure); return emailVoteRequestResult; }