private Post CreateModel(PostBindingModel model, Post post, HotelContext context) { post.Count = post.Count; post.Salary = model.Salary; post.Name = model.Name; return(post); }
public async Task <IActionResult> Create(PostBindingModel postData) { if (ModelState.IsValid) { string filePath = null; //upload image if (postData.ImageFile != null && postData.ImageFile.Length > 0) { filePath = await UploadImageAsync(postData); if (filePath == null) { ModelState.AddModelError("InvoiceFile", "Invalid File Type!"); return(View());//todo } } Post PostToAdd = new Post { Title = postData.Title, Description = postData.Description.Replace("\r\n", "<br />").Replace("\n", "<br />"), ImageFile = filePath, CreatedDate = DateTime.Now, }; _context.Add(PostToAdd); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(postData)); }
public async Task <IHttpActionResult> UpdatePost([FromBody] PostBindingModel model) { try { ApplicationUser creator = this.userRepository.GetOneById(User.Identity.GetUserId()); Post p = this.repository.GetOneById(model.PostId); if (creator == null) { return(BadRequest("Bad token or user does not exist")); } //If the user is an admin or the creator, update the post if (User.IsInRole("Admin") || (p.creator == creator)) { repository.UpdateOne(new Post { Id = model.PostId, Text = model.Text }); return(Ok("Post Updated")); } else { return(BadRequest("Cannot update this post unless you are its creator or an admin")); } } catch { return(BadRequest("Bad Request")); } }
public async Task <IActionResult> AddPost(PostBindingModel model, IFormFile picture) { try { if (!ModelState.IsValid || (picture == null && model.Url == null) || (picture != null && model.Url != null)) { return(View(model)); } if (!this.User.Identity.IsAuthenticated) { return(RedirectToAction("Login", "Account")); } var userId = this.User.GetUserId(); if (picture != null) { model.Picture = ConvertToBytes(picture); } await this.postsLogic.AddPost(model, userId); return(RedirectToAction("GetAllPosts")); } catch (Exception e) { return(View(model)); } }
public async Task <string> UploadImageAsync(PostBindingModel postData) { var ext = Path.GetExtension(postData.ImageFile.FileName).ToLowerInvariant(); if (string.IsNullOrEmpty(ext) || !Constants.permittedExtensions.Contains(ext)) { return(null); } else { string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Day.ToString().PadLeft(2, '0') + "_" + DateTime.Now.Hour.ToString().PadLeft(2, '0') + DateTime.Now.Minute.ToString().PadLeft(2, '0') + DateTime.Now.Second.ToString().PadLeft(2, '0') + "_" + DateTime.Now.Millisecond.ToString().PadLeft(4, '0') + ext; string filePath = Path.Combine(_configuration.GetValue <string>("CustomSettings:PostsUploadPath"), fileName); using (var stream = System.IO.File.Create(filePath)) { await postData.ImageFile.CopyToAsync(stream); UploadImageHelper.ResizeAndSaveImage(stream, Path.Combine(_configuration.GetValue <string>("CustomSettings:PostsUploadPath"), "thumb", fileName)); } return(fileName); } }
public async Task <IHttpActionResult> CreatePost([FromBody] PostBindingModel model) { try { ApplicationUser creator = this.userRepository.GetOneById(User.Identity.GetUserId()); Topic t = this.topicRepository.GetOneById(model.ThreadId); if (creator == null) { return(BadRequest("Bad token or user does not exist")); } //If the user is an admin, the creator or its public or its private and theyre a subscriber, post to topic if (User.IsInRole("Admin") || (t.creator == creator) || t.isPrivate == false || (t.subscribers.Where(x => x.Id == creator.Id).Count() != 1)) { repository.CreateOne(new Post() { ThreadID = model.ThreadId, Text = model.Text, CreatorId = creator.Id, TimeStamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds() }); return(Ok("Post Created")); } else { return(BadRequest("Cannot post to a topic unless you're an admin, it's creator or a subscriber")); } } catch { return(BadRequest("Bad Request")); } }
public async Task <ActionResult> Create(PostBindingModel post) { var model = new PostDataModel() { Titulo = post.Titulo, Descricao = post.Descricao, PerfilId = post.PerfilId }; try { HttpFileCollectionBase files = Request.Files; int fileCount = files.Count; await _blobHelper.SetupCloudBlob(connectionString); var blob = _blobHelper._blobCointainer.GetBlockBlobReference(_blobHelper.GetRandomBlobName(files[0].FileName)); await blob.UploadFromStreamAsync(files[0].InputStream); model.Foto = blob.StorageUri.PrimaryUri.ToString(); await _clientHelper.PostPostAsync(model); return(RedirectToAction("Index")); } catch { return(View()); } }
// GET: Post/Edit/5 public async Task <ActionResult> Edit(int id) { var post = await _clientHelper.GetPostAsync(id); var perfis = await _clientHelper.GetPerfilsAsync(); var perfil = await _clientHelper.GetPerfilAsync(post.PerfilId); var model = new PostBindingModel() { Titulo = post.Titulo, Descricao = post.Descricao, Foto = post.Foto, Id = id, PerfilId = post.PerfilId, PerfilNome = perfil.Nome }; foreach (var item in perfis) { var SelectItem = new SelectListItem() { Value = item.Id.ToString(), Text = item.Nome }; model.Perfis.Add(SelectItem); } return(View(model)); }
public IHttpActionResult Delete(Guid id) { if (ModelState.IsValid) { try { var pbm = new PostBindingModel { Id = id }; var post = Mapper.Map <PostBindingModel, Post>(pbm); _postService.Delete(post.Id); return(Ok()); } catch (Exception ex) { var result = ex.Message; } } else { return(BadRequest(ModelState)); } return(Ok(StatusCode(HttpStatusCode.BadRequest))); }
public IHttpActionResult Update(PostBindingModel pbm) { if (ModelState.IsValid) { try { var post = Mapper.Map <PostBindingModel, Post>(pbm); _postService.Update(post); post = _postService.GetById(post.Id); var pbmUp = Mapper.Map <Post, PostBindingModel>(post); return(Ok(pbmUp)); } catch (Exception ex) { var result = ex.Message; } } else { return(BadRequest(ModelState)); } return(Ok(StatusCode(HttpStatusCode.BadRequest))); }
// POST api/post public IHttpActionResult Post(PostBindingModel post) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var postedBy = _userRepository.FindById(User.Identity.GetUserId()); var model = new Post() { Message = post.Message, PostedBy = postedBy.Id, PostedOn = DateTime.UtcNow, LikedBy = new List <string>(), Comments = new List <Comment>() }; model.PostedBy = User.Identity.GetUserId(); _postRepository.Insert(model); var vModel = new PostViewModel { Id = model.Id, Message = post.Message, PostedBy = AutoMapper.Mapper.Map <ApplicationUser, ApplicationUserViewModel>(postedBy), LikeCount = model.LikeCount, LikedBy = new List <ApplicationUserViewModel>(), Comments = new List <CommentViewModel>() }; return(Ok(vModel)); }
public void Insert(PostBindingModel model) { using (var context = new AutoshowDbContext()) { context.Posts.Add(CreateModel(model, new Post())); context.SaveChanges(); } }
public async Task <IActionResult> Delete(int id, PostBindingModel model) { await this.posts.DeleteAsync(id); this.TempData.AddSuccessMessage(string.Format(WebConstants.DeletedMessage, "Post")); return(Redirect("/")); }
public void Insert(PostBindingModel model) { using (var context = new HotelContext()) { context.Post.Add(CreateModel(model, new Post(), context)); context.SaveChanges(); } }
public async Task AddPost(PostBindingModel post, string userId) { var mappedPost = Mapper.Map <PostBindingModel, Post>(post); mappedPost.PostedOn = DateTime.Now; mappedPost.UserId = userId; await this.postsService.AddPost(mappedPost); }
public IActionResult Create() { var model = new PostBindingModel { Categories = this.categories.All() }; return(View(model)); }
public IHttpActionResult updatePost(PostBindingModel aPost) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // ApplicationDbContext db = new ApplicationDbContext(); return(Ok()); }
public void Delete(PostBindingModel model) { var element = PostStorage.GetElement(new PostBindingModel { Id = model.Id }); if (element == null) { throw new Exception("Элемент не найден"); } PostStorage.Delete(model); }
public void Update(PostBindingModel model) { using (var context = new HotelContext()) { var element = context.Post.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Клиент не найден"); } CreateModel(model, element, context); context.SaveChanges(); } }
public List <PostViewModel> GetFilteredList(PostBindingModel model) { if (model == null) { return(null); } using (var context = new HotelContext()) { return(context.Post .Where(rec => rec.Salary == model.Salary) .Select(CreateModel) .ToList()); } }
public List <PostViewModel> Read(PostBindingModel model) { if (model == null) { return(PostStorage.GetFullList()); } if (model.Id.HasValue) { return(new List <PostViewModel> { PostStorage.GetElement(model) }); } return(PostStorage.GetFilteredList(model)); }
public void Update(PostBindingModel model) { using (var context = new AutoshowDbContext()) { var Post = context.Posts.FirstOrDefault(rec => rec.Id == model.Id); if (Post == null) { throw new Exception("Сотрудник не найден"); } CreateModel(model, Post); context.SaveChanges(); } }
public PostViewModel GetElement(PostBindingModel model) { if (model == null) { return(null); } using (var context = new HotelContext()) { var accounting = context.Post .FirstOrDefault(rec => rec.Id == model.Id || rec.Name == model.Name); return(accounting != null?CreateModel(accounting) : null); } }
public void Delete(PostBindingModel model) { using (var context = new HotelContext()) { Post element = context.Post.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Post.Remove(element); context.SaveChanges(); } else { throw new Exception("Клиент не найден"); } } }
// PUT api/post/5 public IHttpActionResult Put(PostBindingModel post) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var postModel = AutoMapper.Mapper.Map <PostBindingModel, Post>(post); var dbPost = _postRepository.FindById(post.Id); dbPost.Message = postModel.Message; _postRepository.Update(dbPost); return(Ok()); }
public IActionResult Create(PostBindingModel model) { if (!this.User.IsAuthenticated) { return(this.RedirectToLogin()); } if (!this.IsValidModel(model)) { this.ShowError(CreateError); return(this.View()); } this.postService.Create(model.Title, model.Content, this.DbUser.Id); return(this.RedirectToHome()); }
public async Task EditAsync(int id, PostBindingModel model) { var category = this.DbContext.Categories.FirstOrDefault(c => c.Name == model.Category); var post = this.DbContext.Posts.FirstOrDefault(p => p.Id == id); if (post == null) { return; } post.Title = model.Title; post.Category = category; post.Content = model.Content; await this.DbContext.SaveChangesAsync(); }
public void Delete(PostBindingModel model) { using (var context = new AutoshowDbContext()) { Post Post = context.Posts.FirstOrDefault(rec => rec.Id == model.Id); if (Post != null) { context.Posts.Remove(Post); context.SaveChanges(); } else { throw new Exception("Сотрудник не найден"); } } }
public async Task <IActionResult> EditPost(int Id, [FromBody] PostBindingModel model) { if (ModelState.IsValid) { Post post = await _context.Post.Include(p => p.PostTags).FirstAsync(x => x.Id == Id); if (post != null) { post.Title = model.Title; post.Description = model.Description; foreach (var item in post.PostTags) { _context.Remove(item); } try { _context.SaveChanges(); var editedPost = _context.Post.Where(x => x.Id == Id).First(); foreach (var item in model.Tags) { var tag = _context.Tag.FirstOrDefault(x => x.Id == item); if (tag == null) { tag = new Tag { Id = item }; _context.Tag.Add(tag); } editedPost.PostTags.Add(new PostTags { PostId = editedPost.Id, TagId = tag.Id }); } return(Ok()); } catch { return(HttpBadRequest()); } } } return(HttpBadRequest()); }
// GET: Post/Create public async Task <ActionResult> Create() { var perfis = await _clientHelper.GetPerfilsAsync(); var model = new PostBindingModel(); foreach (var item in perfis) { var SelectItem = new SelectListItem() { Value = item.Id.ToString(), Text = item.Nome }; model.Perfis.Add(SelectItem); } return(View(model)); }