public IActionResult AddPost(AdPost newAd) { ManageAds db = new ManageAds(_conn); AdViewModel vm = new AdViewModel(); db.AddNewAd(newAd); idList = HttpContext.Session.Get <List <int> >("adIdSession"); if (idList == null) { idList = new List <int> { newAd.Id }; } else { idList.Add(newAd.Id); } HttpContext.Session.Set <List <int> >("adIdSession", idList); return(Redirect("/Home/Index")); }
public async Task <IActionResult> Edit(short id, [Bind("Id,Titre,Description,file,Link")] AdInput ad) { if (id != ad.Id) { return(NotFound()); } if (ModelState.IsValid) { try { string accessToken = await HttpContext.GetTokenAsync("access_token"); HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); client.BaseAddress = new Uri(Configuration["URLAPI"] + "api/Ads/" + ad.Id); var imageContent = new StreamContent(ad.file.OpenReadStream()); StreamContent streamContent = new StreamContent(ad.file.OpenReadStream()); var memoryStream = new MemoryStream(); await streamContent.CopyToAsync(memoryStream); var bytes = memoryStream.ToArray(); string base64 = Convert.ToBase64String(bytes); double d = base64.Length; AdPost adPost = new AdPost(); adPost.Id = ad.Id; adPost.Titre = ad.Titre; adPost.Description = ad.Description; adPost.file = base64; adPost.fileName = ad.file.FileName; adPost.name = ad.file.Name; adPost.ContentDisposition = ad.file.ContentDisposition; adPost.ContentType = ad.file.ContentType; adPost.Link = ad.Link; string json = await Task.Run(() => JsonConvert.SerializeObject(adPost)); var httpContent = new StringContent(json, Encoding.UTF8, "application/json"); await client.PutAsync(client.BaseAddress, httpContent); } catch (DbUpdateConcurrencyException) { return(NotFound()); } return(RedirectToAction(nameof(Index))); } return(View(ad)); }
public async Task <ActionResult <Ad> > PostAd(AdPost adPost) { string folder = "Ads"; AdInput ad = new AdInput(); ad.Id = adPost.Id; ad.Titre = adPost.Titre; ad.Description = adPost.Description; ad.Link = adPost.Link; byte[] data = Convert.FromBase64String(adPost.file); var stream = new MemoryStream(data); IFormFile file = new FormFile(stream, 0, data.Length, adPost.name, adPost.fileName) { Headers = new HeaderDictionary(), ContentType = adPost.ContentType, ContentDisposition = adPost.ContentDisposition }; ad.file = file; if (ad.file.Length > 0) { if (!Directory.Exists(Path.Combine(_environnement.WebRootPath, folder))) { Directory.CreateDirectory(Path.Combine(_environnement.WebRootPath, folder)); } string filename = "_ad" + DateTime.Now.ToString("_yyyy-MM-dd_HH-mm-ss_") + ad.file.FileName; using (FileStream fileStream = System.IO.File.Create(Path.Combine(_environnement.WebRootPath, folder, filename))) { ad.file.CopyTo(fileStream); fileStream.Flush(); _context.Ads.Add(new Ad { Id = ad.Id, Titre = ad.Titre, Description = ad.Description, AdView = folder + "/" + filename, Link = ad.Link }); _context.SaveChanges(); } await _context.SaveChangesAsync(); } return(CreatedAtAction("GetAd", new { id = ad.Id }, ad)); }
public IActionResult AddPost(AdPost newAd) { ManageAds db = new ManageAds(_conn); AdViewModel vm = new AdViewModel(); db.AddNewAd(newAd); string idList = Request.Cookies["ids"]; if (idList == null) { idList = $"{newAd.Id}"; } else { idList += $",{newAd.Id}"; } Response.Cookies.Append("ids", $"{idList}"); return(Redirect("/Cookie/Index")); }
public bool UpdateEvent(AdPost Ad) { throw new NotImplementedException(); }
public bool RemoveAd(AdPost Ad) { throw new NotImplementedException(); }
public async Task <IActionResult> PutAd(short id, AdPost adPost) { if (id != adPost.Id) { return(BadRequest()); } string folder = "Ads"; AdInput ad = new AdInput(); ad.Id = adPost.Id; ad.Titre = adPost.Titre; ad.Description = adPost.Description; ad.Link = adPost.Link; byte[] data = Convert.FromBase64String(adPost.file); var stream = new MemoryStream(data); IFormFile file = new FormFile(stream, 0, data.Length, adPost.name, adPost.fileName) { Headers = new HeaderDictionary(), ContentType = adPost.ContentType, ContentDisposition = adPost.ContentDisposition }; ad.file = file; if (ad.file.Length > 0) { if (!Directory.Exists(Path.Combine(_environnement.WebRootPath, folder))) { Directory.CreateDirectory(Path.Combine(_environnement.WebRootPath, folder)); } string filename = "_ad" + DateTime.Now.ToString("_yyyy-MM-dd_HH-mm-ss_") + ad.file.FileName; using (FileStream fileStream = System.IO.File.Create(Path.Combine(_environnement.WebRootPath, folder, filename))) { ad.file.CopyTo(fileStream); fileStream.Flush(); _context.Entry(new Ad { Id = ad.Id, Titre = ad.Titre, Description = ad.Description, AdView = folder + "/" + filename, Link = ad.Link }).State = EntityState.Modified; } } try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AdExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }