API
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CampaignController.cs 4.8KB

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