public async Task <ActionResult <ResponseViewModel> > Auth([FromBody] UserAuthViewModel request) { if (!await _userService.IsUserExists(request.Username)) { return(_responseCreator.CreateFailure("This user doesn't exist")); } var user = await _userService.AuthUser(request.Username, request.Password); var tokenModel = _mapper.Map <UserTokenViewModel>(user); tokenModel.Token = _tokenCreator.CreateToken(JsonConvert.SerializeObject(user)); return(_responseCreator.CreateSuccess(tokenModel)); }
public ActionResult <ResponseViewModel> GetLanguages([FromBody] TokenViewModel tokenModel) { var user = JsonConvert.DeserializeObject <User>(_tokenCreator.DecodeToken(tokenModel.Token)); if (user == null) { return(_responseCreator.CreateFailure("Bad user token!")); } var languages = _languageService.GetAllLanguages().ToArray(); return(_responseCreator.CreateSuccess(languages)); }
public async Task <ResponseViewModel> AddSnippet([FromBody] AddSnippetViewModel addSnippetViewModel) { var user = JsonConvert.DeserializeObject <User>(_tokenCreator.DecodeToken(addSnippetViewModel.Token)); if (user == null) { return(_responseCreator.CreateFailure("Bad user token!")); } var tags = await _tagService.AddOrUpdateTags(addSnippetViewModel.Tags); var language = _languageService.GetLanguageByContent(addSnippetViewModel.Language); if (language == null) { return(_responseCreator.CreateFailure("Bad language!")); } var snippet = await _snippetService.AddSnippet(addSnippetViewModel.Name, addSnippetViewModel.Description, addSnippetViewModel.Code, user, language, tags); return(_responseCreator.CreateSuccess("Snippet added successfully")); }