選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ModelFactory.cs 1.9KB

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