Guest User

Untitled

a guest
Apr 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. $("#createLetter").on("click", CreateLetter);
  2.  
  3. function CreateLetter() {
  4. $.ajax({
  5. type: "POST",
  6. url: "/Letters/CreateLetter",
  7. data: {
  8. EntityType: "@overview.EntityType",
  9. EntityId: @overview.EntityId,
  10. Recipient: $("#Recipient").val(),
  11. TemplatesLocation: $("#templatePath").val(),
  12. SaveAs: $("#saveAs").val()
  13. },
  14. async: false,
  15. success: openLetter
  16. });
  17. }
  18.  
  19. function openLetter(data) {
  20. openFile(data);
  21. window.location.reload(false);
  22.  
  23. }
  24.  
  25. [ValidateInput(false)]
  26. [HttpPost]
  27. public JsonResult CreateLetter(CreateLetter input)
  28. {
  29. Recipient obj = logic.SplitRecipientInput(input.Recipient);
  30.  
  31. input.RecipientId = obj.RecipientId;
  32. input.RecipientType = obj.Type;
  33.  
  34. input.Username = Helpers.GetLoggedInUser();
  35.  
  36. var x = logic.CreateLetter(input);
  37.  
  38. if (x.Success == 1)
  39. {
  40.  
  41. return Json(x.Data, JsonRequestBehavior.AllowGet);
  42. }
  43.  
  44. else
  45. {
  46. return Json("Error", JsonRequestBehavior.AllowGet);
  47. }
  48. }
  49.  
  50. public CreatedLetter CreateLetter(CreateLetter input)
  51. {
  52. CreatedLetter response = new CreatedLetter();
  53. Parameters.Add("TemplatePath", GetApiValue(input.TemplatesLocation));
  54. Parameters.Add("EntityType", GetApiValue(input.EntityType));
  55. Parameters.Add("EntityId", GetApiValue(input.EntityId));
  56. Parameters.Add("RecipientId", GetApiValue(input.RecipientId));
  57. Parameters.Add("RecipientType", GetApiValue(input.RecipientType));
  58. Parameters.Add("Username", GetApiValue(input.Username));
  59. Parameters.Add("SaveAs", GetApiValue(input.SaveAs));
  60.  
  61. response = Api.WebRequest<CreatedLetter>("CreateLetters", Parameters, Method.POST) as CreatedLetter;
  62.  
  63. return response;
  64. }
  65.  
  66. [ActionName("CreateLetter")]
  67. [HttpPost]
  68. public ApiResponse CreateLetter(LetterCreateInput input)
  69. {
  70. try
  71. {
  72. LetterTemplateLogic logic = new LetterTemplateLogic();
  73.  
  74. Random r = new Random();
  75. var randomId = r.Next(100000, 999999);
  76.  
  77. string fileName = string.Format("{0} - {1}", randomId, input.SaveAs);
  78. input.SaveAs = fileName;
  79.  
  80. // Get all objects for Letter
  81. List<object> objs = logic.TemplateObjectsRetriever(input.EntityId, input.EntityType, input.Username, randomId);
  82.  
  83. objs.Add(logic.GetRecipient(input.RecipientId, input.RecipientType));
  84.  
  85. // Get save location
  86. string saveLocation = logic.LetterLocationResolver(input.EntityId, input.EntityType);
  87.  
  88. var data = logic.OpenAndUpdateTemplate(objs, input.TemplatePath, input.SaveAs, saveLocation, FileExtension);
  89.  
  90. AttachmentInput letterAttachment = new AttachmentInput();
  91. letterAttachment.Id = input.EntityId;
  92. letterAttachment.FileTypeId = 1;
  93. letterAttachment.Path = data;
  94. letterAttachment.Username = input.Username;
  95. letterAttachment.Description = fileName;
  96. letterAttachment.EntityType = input.EntityType;
  97.  
  98. logic.InsertLetterAttachment(letterAttachment);
  99.  
  100. return ApiResponse.Return(data);
  101. }
  102. catch (Exception ex)
  103. {
  104. return ApiResponse.Error(ex);
  105. }
  106. }
Add Comment
Please, Sign In to add comment