123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- 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<IActionResult> 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();
- }
- }
- }
|