API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CampaignController.cs 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using System;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Threading.Tasks;
  8. using System.Transactions;
  9. using UnivateProperties_API.Model.Campaigns;
  10. using UnivateProperties_API.Repository.Campaigns;
  11. namespace UnivateProperties_API.Controllers.Campaigns
  12. {
  13. [Route("api/[controller]")]
  14. [ApiController]
  15. public class CampaignController : ControllerBase
  16. {
  17. private readonly ICampaignRepository _Repo;
  18. public CampaignController(ICampaignRepository _repo)
  19. {
  20. _Repo = _repo;
  21. }
  22. [HttpGet]
  23. public IActionResult Get()
  24. {
  25. return new OkObjectResult(_Repo.GetAll());
  26. }
  27. [HttpGet("{id}")]
  28. public IActionResult Get(int id)
  29. {
  30. return new OkObjectResult(_Repo.GetDetailed(x => x.Id == id));
  31. }
  32. [HttpGet("GetDTO/{id}")]
  33. public IActionResult GetDTO(int id)
  34. {
  35. return new OkObjectResult(_Repo.GetDTO(x => x.Id == id));
  36. }
  37. [HttpGet("GetLandingPage/{id}")]
  38. public IActionResult GetLandingPage(int id)
  39. {
  40. return new OkObjectResult(_Repo.GetCampaignHTML(id));
  41. }
  42. [HttpGet("GetCampaignItems/{id}")]
  43. public IActionResult GetCampaignItems(int id)
  44. {
  45. return new OkObjectResult(_Repo.GetCampaignItems(id));
  46. }
  47. [HttpGet("GetCampaignPlaceHolders/{id}")]
  48. public IActionResult GetCampaignPlaceHolders(int id)
  49. {
  50. return new OkObjectResult(_Repo.GetCampaignPlaceHolders(id));
  51. }
  52. [HttpPost]
  53. public IActionResult Post([FromBody] Campaign campaign)
  54. {
  55. using (var scope = new TransactionScope())
  56. {
  57. _Repo.Insert(campaign);
  58. scope.Complete();
  59. return CreatedAtAction(nameof(Get), new { id = campaign.Id }, campaign);
  60. }
  61. }
  62. [HttpPost("FileUpload")]
  63. public async Task<IActionResult> Post(IFormCollection files)
  64. {
  65. //string url = "http://localhost:8080";
  66. //string url = "http://training.provision-sa.com:122";
  67. string url = "https://www.univateproperties.co.za";
  68. string fileName = "";
  69. try
  70. {
  71. long size = files.Files.Sum(f => f.Length);
  72. //string path = Path.Combine(@"C:\Users\7675\Desktop", "Uploads");
  73. //string path = Path.Combine(@"C:\inetpub\wwwroot\UniVatePropertiesNew", "lp");
  74. string path = Path.Combine(@"C:\inetpub\wwwroot\UniVateProperties", "lp");
  75. foreach (var formFile in files.Files)
  76. {
  77. if (formFile.Length > 0)
  78. {
  79. fileName = formFile.FileName;
  80. string fullPath = Path.Combine(path, fileName);
  81. using (var stream = new FileStream(fullPath, FileMode.Create))
  82. {
  83. await formFile.CopyToAsync(stream);
  84. }
  85. }
  86. }
  87. UploadCampaign camp = new UploadCampaign();
  88. camp.FileName = fileName;
  89. camp.FileLink = url + "/lp/" + fileName;
  90. _Repo.addUploadCampaign(camp);
  91. return Ok(new
  92. {
  93. archives = files.Files.Count,
  94. link = url + "/lp/" + fileName,
  95. size,
  96. path
  97. });
  98. }
  99. catch (Exception ex)
  100. {
  101. return StatusCode((int)HttpStatusCode.InternalServerError, ex);
  102. }
  103. }
  104. [HttpGet("GetUploaded")]
  105. public IActionResult GetUploaded()
  106. {
  107. return new OkObjectResult(_Repo.GetUploadedCampaigns());
  108. }
  109. [HttpPut]
  110. public IActionResult Put(Campaign campaign)
  111. {
  112. if (campaign != null)
  113. {
  114. using (var scope = new TransactionScope())
  115. {
  116. _Repo.Update(campaign);
  117. scope.Complete();
  118. return new OkResult();
  119. }
  120. }
  121. return new NoContentResult();
  122. }
  123. [HttpDelete("DeleteUploaded/{id}")]
  124. public IActionResult DeleteUploaded(int id)
  125. {
  126. _Repo.RemoveUploadedAtId(id);
  127. return new OkResult();
  128. }
  129. [HttpDelete("{id}")]
  130. public IActionResult Delete(int id)
  131. {
  132. _Repo.RemoveAtId(id);
  133. return new OkResult();
  134. }
  135. }
  136. }