Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.55 KB | None | 0 0
  1. using Common.Logging;
  2. using Common.Notifications;
  3. using NHibernate;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7.  
  8. namespace Common.Commands.Revelation
  9. {
  10.     public class RequestDecideHandler : BaseHandler
  11.     {
  12.         private readonly Logger _logger = new Logger(typeof(RequestDecideHandler).FullName);
  13.  
  14.         public RequestDecideHandler(ISession session = null) : base(session)
  15.         {
  16.  
  17.         }
  18.  
  19.         public void Handle(RequestDecideCommand command)
  20.         {
  21.             var revelation = _session.Query<Models.Revelation>()
  22.                 .Where(r => r.User.Id == command.UserId && r.Offer == command.Offer)
  23.                 .Single();
  24.  
  25.             if (command.Decision == true)
  26.             {
  27.                 var rejectedRevelations = _session.Query<Models.Revelation>()
  28.                     .Where(r => r.User.Id != command.UserId && r.Offer == command.Offer)
  29.                     .ToList();
  30.  
  31.                 using (var transaction = _session.BeginTransaction())
  32.                 {
  33.                     command.Offer.Status = Models.OfferStatus.Finalized;
  34.                     revelation.Status = Models.RevelationStatus.Finalized;
  35.                    
  36.                     foreach(var rejectedRevelation in rejectedRevelations)
  37.                     {
  38.                         RejectRevelation(rejectedRevelation);
  39.                         SendRejectNotification(rejectedRevelation);
  40.                     }
  41.  
  42.                     SendAcceptNotification(revelation, command.UserId);
  43.  
  44.                     _session.SaveOrUpdate(command.Offer);
  45.                     _session.SaveOrUpdate(revelation);
  46.  
  47.                     transaction.Commit();
  48.                 }
  49.             }
  50.             else
  51.             {
  52.                 using (var transaction = _session.BeginTransaction())
  53.                 {
  54.                     RejectRevelation(revelation);
  55.                     transaction.Commit();
  56.                 }
  57.             }
  58.         }
  59.  
  60.         private void RejectRevelation(Models.Revelation revelation)
  61.         {
  62.             revelation.Status = Models.RevelationStatus.Rejected;
  63.        
  64.             _session.SaveOrUpdate(revelation);
  65.         }
  66.  
  67.         private void SendAcceptNotification(Models.Revelation revelation, long userId)
  68.         {
  69.             var receiver = _session.Query<Models.User>().FirstOrDefault(x => x.Id == userId);
  70.             SendNotification(revelation, receiver, NotificationType.Accept);
  71.         }
  72.  
  73.         private void SendRejectNotification(Models.Revelation revelation)
  74.         {
  75.             SendNotification(revelation, revelation.User, NotificationType.Reject);
  76.         }
  77.  
  78.         private void SendNotification(Models.Revelation revelation, Models.User receiver, NotificationType type)
  79.         {
  80.             var notificationTemplate = NotificationFactory.GetNotification(type);
  81.             var message = notificationTemplate.GetMessage(new Dictionary<NotificationParameterName, string>
  82.             {
  83.                 { NotificationParameterName.CompanyName, revelation.Offer.User.InvoiceName }
  84.             });
  85.  
  86.             if (receiver != null)
  87.             {
  88.                 var notification = new Models.Notification()
  89.                 {
  90.                     Offer = revelation.Offer,
  91.                     User = receiver,
  92.                     Message = message,
  93.                     CreateTime = DateTime.Now,
  94.                     IsRead = false,
  95.                     IsToasted = false
  96.                 };
  97.  
  98.                 _session.SaveOrUpdate(notification);
  99.             }
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement