Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Common.Logging;
- using Common.Notifications;
- using NHibernate;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Common.Commands.Revelation
- {
- public class RequestDecideHandler : BaseHandler
- {
- private readonly Logger _logger = new Logger(typeof(RequestDecideHandler).FullName);
- public RequestDecideHandler(ISession session = null) : base(session)
- {
- }
- public void Handle(RequestDecideCommand command)
- {
- var revelation = _session.Query<Models.Revelation>()
- .Where(r => r.User.Id == command.UserId && r.Offer == command.Offer)
- .Single();
- if (command.Decision == true)
- {
- var rejectedRevelations = _session.Query<Models.Revelation>()
- .Where(r => r.User.Id != command.UserId && r.Offer == command.Offer)
- .ToList();
- using (var transaction = _session.BeginTransaction())
- {
- command.Offer.Status = Models.OfferStatus.Finalized;
- revelation.Status = Models.RevelationStatus.Finalized;
- foreach(var rejectedRevelation in rejectedRevelations)
- {
- RejectRevelation(rejectedRevelation);
- SendRejectNotification(rejectedRevelation);
- }
- SendAcceptNotification(revelation, command.UserId);
- _session.SaveOrUpdate(command.Offer);
- _session.SaveOrUpdate(revelation);
- transaction.Commit();
- }
- }
- else
- {
- using (var transaction = _session.BeginTransaction())
- {
- RejectRevelation(revelation);
- transaction.Commit();
- }
- }
- }
- private void RejectRevelation(Models.Revelation revelation)
- {
- revelation.Status = Models.RevelationStatus.Rejected;
- _session.SaveOrUpdate(revelation);
- }
- private void SendAcceptNotification(Models.Revelation revelation, long userId)
- {
- var receiver = _session.Query<Models.User>().FirstOrDefault(x => x.Id == userId);
- SendNotification(revelation, receiver, NotificationType.Accept);
- }
- private void SendRejectNotification(Models.Revelation revelation)
- {
- SendNotification(revelation, revelation.User, NotificationType.Reject);
- }
- private void SendNotification(Models.Revelation revelation, Models.User receiver, NotificationType type)
- {
- var notificationTemplate = NotificationFactory.GetNotification(type);
- var message = notificationTemplate.GetMessage(new Dictionary<NotificationParameterName, string>
- {
- { NotificationParameterName.CompanyName, revelation.Offer.User.InvoiceName }
- });
- if (receiver != null)
- {
- var notification = new Models.Notification()
- {
- Offer = revelation.Offer,
- User = receiver,
- Message = message,
- CreateTime = DateTime.Now,
- IsRead = false,
- IsToasted = false
- };
- _session.SaveOrUpdate(notification);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement