public ActionResult PostNewTelereet(NewTelereetViewModel model) { try { var userId = User.Identity.GetUserId(); if (string.IsNullOrEmpty(userId)) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest, string.Join(", ", "User must be logged for new Telereet")); } if (ModelState.IsValid) { var newPost = CreateTelereet(userId, model); this.data.Telreets.Add(newPost); this.data.SaveChanges(); var newTelereet = CreateTelereetViewModel(newPost); return PartialView("_TelereetPartial", newTelereet); } else { var errorsMessages = ModelState.Values.Where(v => v.Errors.Count > 0) .SelectMany(v => v.Errors.Select(e => e.ErrorMessage)); return new HttpStatusCodeResult(HttpStatusCode.BadRequest, string.Join(", ", errorsMessages)); } } catch { return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Database connection lost"); } }
private Telereet CreateTelereet(string userId, NewTelereetViewModel model) { var newPost = new Telereet { PostedOn = DateTime.Now, Author = this.data.Users.All().FirstOrDefault(u => u.Id == userId), Text = model.Text, Tags = GetTags(model.Tags).ToList() }; return newPost; }