Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ModelFactory.cs 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Threading.Tasks;
  3. using SecureSharing.Business.Interfaces;
  4. using SecureSharing.Models;
  5. namespace SecureSharing.Infrastructure;
  6. public sealed class ModelFactory : IModelFactory
  7. {
  8. private readonly IMessageService _messageService;
  9. public ModelFactory(IMessageService messageService)
  10. {
  11. _messageService = messageService;
  12. }
  13. public async Task<LinkModel> PrepareLinkVM(int id, bool? share)
  14. {
  15. //share is true when the link is created
  16. LinkModel model = null;
  17. try
  18. {
  19. var message = await _messageService.GetById(id);
  20. model = new LinkModel { message = new MessageModel { Id = id, Text = message.Text }, Share = share };
  21. model.IsValid = message.IsValid;
  22. if (model.IsValid)
  23. {
  24. if (message.ExpiryDate != null)
  25. {
  26. model.TimeLeft = message.ExpiryDate - DateTime.UtcNow;
  27. if (message.ExpiryDate <= DateTime.UtcNow)
  28. {
  29. await _messageService.InvalidateMessage(message.Id);
  30. model.IsValid = false;
  31. }
  32. }
  33. else
  34. {
  35. //ONE_TIME sharing: make the message invalid now so that it can't be accessed next time
  36. if (share == null || share.Value == false) await _messageService.InvalidateMessage(message.Id);
  37. }
  38. }
  39. }
  40. catch (Exception e)
  41. {
  42. model = new LinkModel { IsValid = false };
  43. }
  44. return model;
  45. }
  46. //public MessageModel PrepareMessageVM(MessageDto message)
  47. //{
  48. // throw new System.NotImplementedException();
  49. //}
  50. }