public async Task <List <string> > PostAd(PostModel model, string un = "") { ListHell.ad na = new ListHell.ad(); if (un != "") { var user = await context.Users.Where(w => w.UserName == un).FirstOrDefaultAsync(); na.Users.Add(user); } na.lid = model.lid; na.catid = model.catid; na.title = model.title; na.description = model.description; na.phone = model.phone; na.address = model.address; na.amount = model.amount; na.datetime = DateTime.Now; na.email = model.email; context.ads.Add(na); context.SaveChanges(); int x = 0; ListHell.Image i = new ListHell.Image(); List <string> lst = new List <string>(); foreach (var o in model.images) { if (o != null) { i.src = "../ai/" + "_" + generateName() + "_" + o.FileName;//change it with guide+jpg lst.Add(i.src); if (x == 0) { i.defaulti = true; } else { i.defaulti = false; } try { o.SaveAs(HttpContext.Current.Server.MapPath(i.src)); } catch (Exception ex) { } i.adid = na.adid; na.Images.Add(i); context.Images.Add(i); context.SaveChanges(); } x++; } return(lst); }
public async Task <bool> DeleteUserAd(int adid, int catid, int lid, string UserName) { try { string id = await context.Users.Where(w => w.UserName == UserName).Select(w => w.Id).FirstOrDefaultAsync(); ListHell.ad a = new ListHell.ad { adid = adid, catid = catid, lid = lid }; a.Users.Add(new User { Id = id }); List <ListHell.Image> i = await context.Images.Where(w => w.adid == adid).ToListAsync(); a.Images = i; context.ads.Attach(a); context.ads.Remove(a); context.SaveChanges(); return(true); } catch (Exception ex) { return(false); } }