using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.IO; using System.Linq; using System.Net; using System.Threading.Tasks; using System.Transactions; using UnivateProperties_API.Model.Campaigns; using UnivateProperties_API.Repository.Campaigns; namespace UnivateProperties_API.Controllers.Campaigns { [Route("api/[controller]")] [ApiController] public class CampaignController : ControllerBase { private readonly ICampaignRepository _Repo; public CampaignController(ICampaignRepository _repo) { _Repo = _repo; } [HttpGet] public IActionResult Get() { return new OkObjectResult(_Repo.GetAll()); } [HttpGet("{id}")] public IActionResult Get(int id) { return new OkObjectResult(_Repo.GetDetailed(x => x.Id == id)); } [HttpGet("GetDTO/{id}")] public IActionResult GetDTO(int id) { return new OkObjectResult(_Repo.GetDTO(x => x.Id == id)); } [HttpGet("GetLandingPage/{id}")] public IActionResult GetLandingPage(int id) { return new OkObjectResult(_Repo.GetCampaignHTML(id)); } [HttpGet("GetCampaignItems/{id}")] public IActionResult GetCampaignItems(int id) { return new OkObjectResult(_Repo.GetCampaignItems(id)); } [HttpGet("GetCampaignPlaceHolders/{id}")] public IActionResult GetCampaignPlaceHolders(int id) { return new OkObjectResult(_Repo.GetCampaignPlaceHolders(id)); } [HttpPost] public IActionResult Post([FromBody] Campaign campaign) { using (var scope = new TransactionScope()) { _Repo.Insert(campaign); scope.Complete(); return CreatedAtAction(nameof(Get), new { id = campaign.Id }, campaign); } } [HttpPost("FileUpload")] public async Task Post(IFormCollection files) { //string url = "http://localhost:8080"; //string url = "http://training.provision-sa.com:122"; string url = "https://www.univateproperties.co.za"; string fileName = ""; try { long size = files.Files.Sum(f => f.Length); //string path = Path.Combine(@"C:\Users\7675\Desktop", "Uploads"); //string path = Path.Combine(@"C:\inetpub\wwwroot\UniVatePropertiesNew", "lp"); string path = Path.Combine(@"C:\inetpub\wwwroot\UniVateProperties", "lp"); foreach (var formFile in files.Files) { if (formFile.Length > 0) { fileName = formFile.FileName; string fullPath = Path.Combine(path, fileName); using (var stream = new FileStream(fullPath, FileMode.Create)) { await formFile.CopyToAsync(stream); } } } UploadCampaign camp = new UploadCampaign(); camp.FileName = fileName; camp.FileLink = url + "/lp/" + fileName; _Repo.addUploadCampaign(camp); return Ok(new { archives = files.Files.Count, link = url + "/lp/" + fileName, size, path }); } catch (Exception ex) { return StatusCode((int)HttpStatusCode.InternalServerError, ex); } } [HttpGet("GetUploaded")] public IActionResult GetUploaded() { return new OkObjectResult(_Repo.GetUploadedCampaigns()); } [HttpPut] public IActionResult Put(Campaign campaign) { if (campaign != null) { using (var scope = new TransactionScope()) { _Repo.Update(campaign); scope.Complete(); return new OkResult(); } } return new NoContentResult(); } [HttpDelete("DeleteUploaded/{id}")] public IActionResult DeleteUploaded(int id) { _Repo.RemoveUploadedAtId(id); return new OkResult(); } [HttpDelete("{id}")] public IActionResult Delete(int id) { _Repo.RemoveAtId(id); return new OkResult(); } } }