public async Task <IActionResult> FollowProfile([FromRoute] string username) { if (string.IsNullOrWhiteSpace(username)) { return(NotFound()); } try { var tokenHeader = Request.Headers["Authorization"]; var token = tokenHeader.Count > 0 ? tokenHeader.First().Split(' ')[1] : null; User currentUser = null; if (!string.IsNullOrWhiteSpace(token)) { currentUser = UserService.GetCurrentUser(token); } var result = await FavoritesService.AddFavoriteProfile(username, token); return(result ? Ok() : (IActionResult)Unauthorized()); } catch (Exception ex) { var genericErrorModel = new GenericErrorModel(new GenericErrorModelErrors(new[] { ex.ToString() })); return(BadRequest(genericErrorModel)); } }
public async Task <IActionResult> Put([FromRoute] string slug, [FromBody] UpdateArticleRequest updateArticleRequest) { try { var tokenHeader = Request.Headers["Authorization"]; var token = tokenHeader.Count > 0 ? tokenHeader.First().Split(' ')[1] : null; User currentUser = null; if (!string.IsNullOrWhiteSpace(token)) { currentUser = UserService.GetCurrentUser(token); } if (currentUser == null) { return(Unauthorized()); } var article = await ArticleService.UpdateArticle(updateArticleRequest.Article, slug, token); return(article != null ? Ok(new SingleArticleResponse { Article = new ArticleModel( article.Slug, article.Title, article.Description, article.Body, article.ArticleTags.Select(t => t.Tag.TagName).ToList(), article.CreatedAt, article.UpdatedAt, article.GetFavorited(currentUser), article.FavoritesCount, article.Author.Profile ) }) : (IActionResult)BadRequest("Not updated.")); } catch (ApplicationException ae) when(ae.Message == "Not found.") { return(NotFound()); } catch (Exception ex) { var genericErrorModel = new GenericErrorModel(new GenericErrorModelErrors(new[] { ex.ToString() })); return(BadRequest(genericErrorModel)); } }
public IActionResult Login([FromBody] LoginUserRequest userRequest) { try { var user = UserService.LoginUser(userRequest.User); return(user != null ? Ok(new UserResponse { User = new UserModel(user.Email, user.Token, user.Username, user.Bio, user.Image) }) : (IActionResult)Unauthorized()); } catch (Exception ex) { var genericErrorModel = new GenericErrorModel(new GenericErrorModelErrors(new[] { ex.ToString() })); return(BadRequest(genericErrorModel)); } }
public IActionResult Get(string slug) { if (string.IsNullOrWhiteSpace(slug)) { return(NotFound()); } try { var tokenHeader = Request.Headers["Authorization"]; var token = tokenHeader.Count > 0 ? tokenHeader.First().Split(' ')[1] : null; User currentUser = null; if (!string.IsNullOrWhiteSpace(token)) { currentUser = UserService.GetCurrentUser(token); } var article = ArticleService.GetArticle(slug); return(article != null ? Ok(new SingleArticleResponse { Article = new ArticleModel( article.Slug, article.Title, article.Description, article.Body, article.ArticleTags.Select(t => t.Tag.TagName).ToList(), article.CreatedAt, article.UpdatedAt, article.GetFavorited(currentUser), article.FavoritesCount, article.Author.Profile ) }) : (IActionResult)NotFound()); } catch (Exception ex) { var genericErrorModel = new GenericErrorModel(new GenericErrorModelErrors(new[] { ex.ToString() })); return(BadRequest(genericErrorModel)); } }
public IActionResult GetTags() { try { var tags = ArticleService.GetTags(TagSort.Name); return(Ok(new TagsResponse { Tags = tags.Select(t => t.TagName).ToList(), Counts = tags.Select(t => new { t.TagName, t.ArticleTag.Count }).ToList() })); } catch (Exception ex) { var genericErrorModel = new GenericErrorModel(new GenericErrorModelErrors(new[] { ex.ToString() })); return(BadRequest(genericErrorModel)); } }
public override void OnException(HttpActionExecutedContext context) { Guid errorId = Guid.NewGuid(); Logger.Error(context.Exception, $"Unhandled error. ErrorId is {errorId}. Controller is {context.ActionContext.ControllerContext.Controller}. StackTrace: {context.Exception.StackTrace}"); var response = new GenericErrorModel { Message = string.Format(GenericErrorText, errorId), ErrorId = errorId, #if DEBUG StackTrace = context.Exception.StackTrace, #endif }; context.Response = context.Request.CreateResponse(HttpStatusCode.InternalServerError, response); context.Exception = null; }
public async Task <IActionResult> Put([FromBody] UpdateUserRequest updateRequest) { try { var user = await UserService.UpdateUser(updateRequest.User); return(Ok(new UserResponse { User = new UserModel(user.Email, null, user.Username, user.Bio, user.Image) })); } catch (ApplicationException ae) when(ae.Message == "Not found.") { return(NotFound()); } catch (Exception ex) { var genericErrorModel = new GenericErrorModel(new GenericErrorModelErrors(new[] { ex.ToString() })); return(UnprocessableEntity(genericErrorModel)); } }
public ActionResult Send(NewMessageModel chatMessage) { if (ModelState.IsValid) { _messageRepo.Insert(new ChatMessage { SendTime = DateTimeOffset.UtcNow, Text = chatMessage.Message, UserName = chatMessage.UserName }); var model = new MessageListModel { Messages = _messageRepo.GetLatestAfterId(chatMessage.LastCheckedId ?? 0).Select(m => new ChatMessageDto(m)) }; return(Json(model)); } GenericErrorModel errorModel = new GenericErrorModel().SetModelErrors(ViewData); return(Json(errorModel)); }
public override void OnActionExecuting(ActionExecutingContext actionContext) { var request = actionContext.HttpContext.Request; if (!request.IsAjaxRequest()) { actionContext.HttpContext.Response.Clear(); actionContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.NotAcceptable; GenericErrorModel ajaxErrorModel = new GenericErrorModel(); ajaxErrorModel.AddMessage("Invalid request"); actionContext.Result = new JsonResult { Data = ajaxErrorModel, JsonRequestBehavior = JsonRequestBehavior.AllowGet, ContentEncoding = System.Text.Encoding.UTF8, ContentType = "application/json", }; } }
public async Task <IActionResult> Post([FromBody] NewUserRequest registerRequest) { try { var user = await UserService.RegisterUser(registerRequest.User); return(Ok(new UserResponse { User = new UserModel(user.Email, user.Token, user.Username, user.Bio, user.Image) })); } catch (ApplicationException ae) when(ae.Message == "409") { var genericErrorModel = new GenericErrorModel(new GenericErrorModelErrors(new[] { "User exists." })); return(UnprocessableEntity(genericErrorModel)); } catch (Exception ex) { var genericErrorModel = new GenericErrorModel(new GenericErrorModelErrors(new[] { ex.ToString() })); return(UnprocessableEntity(genericErrorModel)); } }