public void Execute(InsertPostDto request) { if (Context.Posts.Any(m => m.Name == request.Name)) { throw new EntityAlreadyExists(); } //if (!Context.Posts.Any(p=>p.UserId == request.UserId)) { // throw new EntityNoFound(); //} if (!Context.Posts.Any(p => p.CategoryPostId == request.CategoryId)) { throw new EntityNoFound(); } var post = new Domen.Post { Name = request.Name, Text = request.Text, CategoryPostId = request.CategoryId, UserId = request.UserId }; //var pic = new Domen.Picture(); //pic.Name = request.FileName; Context.Add(post); Context.SaveChanges(); // int LastPostId = post.Id; Domen.Picture pic = new Domen.Picture { Name = request.FileName, PostId = post.Id }; Context.Add(pic); Context.SaveChanges(); }
public ActionResult Create(AddPostcs p) { var ext = Path.GetExtension(p.Image.FileName); if (!FileUpload.AllowedExtensions.Contains(ext)) { return(UnprocessableEntity("Image extension is not allowed.")); } try { var newFileName = Guid.NewGuid().ToString() + "_" + p.Image.FileName; var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", newFileName); p.Image.CopyTo(new FileStream(filePath, FileMode.Create)); var post = new InsertPostDto { Name = p.Name, FileName = newFileName, Text = p.Text, CategoryId = p.CategoryId, UserId = p.UserId, }; _addPost.Execute(post); return(RedirectToAction("Index")); } catch (EntityNoFound n) { return(NotFound()); } catch (EntityAlreadyExists b) { return(NotFound()); } catch (Exception e) { TempData["greska"] = "Doslo je do greske."; } return(View()); }
public void Execute(InsertPostDto request) { var post = new Post { Name = request.Name, UserId = request.UserId, Description = request.Text, CetegoryId = request.CategoryId }; _context.Posts.Add(post); _context.SaveChanges(); Picture pic = new Picture { Name = request.FileName, IdPost = post.Id }; _context.Add(pic); _context.SaveChanges(); }
public IActionResult Post([FromForm] AddPost p) { var ext = Path.GetExtension(p.Image.FileName); if (!FileUpload.AllowedExtensions.Contains(ext)) { return(UnprocessableEntity("Image extension is not allowed.")); } try { var newFileName = Guid.NewGuid().ToString() + "_" + p.Image.FileName; var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images", newFileName); p.Image.CopyTo(new FileStream(filePath, FileMode.Create)); var post = new InsertPostDto { Name = p.Name, FileName = newFileName, Text = p.Text, CategoryId = p.CategoryId, UserId = p.UserId, }; _addPost.Execute(post); return(StatusCode(201)); } catch (EntityAlredyExists) { return(NotFound()); } catch (Exception e) { return(StatusCode(500, "An error has occured.")); } }