public StoryAggregate Update(string Token, StoryAggregate StoryInstance) { try { var Handler = new JwtSecurityTokenHandler(); Token = Token.Replace("Bearer", "").Trim(); var JsonToken = Handler.ReadJwtToken(Token); string Id = JsonToken.Claims.First(c => c.Type == "id").Value; Console.WriteLine("============================================"); Console.WriteLine("============================================"); Console.WriteLine("============================================"); Console.WriteLine(JsonConvert.SerializeObject(StoryInstance)); Story Story = this.StoryContextInstance.Stories.SingleOrDefault(s => s.Id == StoryInstance.StoryInfo.Id); this.StoryContextInstance.Stories.Update(Story); List <Tag> Tags = new List <Tag>(); Tags.AddRange(StoryInstance.Tags); Tags.ForEach(Tag => { StoryTag StoryTag = new StoryTag(); StoryTag.TagId = Tag.Id; StoryTag.StoryId = Story.Id; if (this.StoryContextInstance.StoryTags.SingleOrDefault(st => st.TagId == StoryTag.TagId && st.StoryId == StoryTag.StoryId) == null) { this.StoryContextInstance.StoryTags.Add(StoryTag); } }); this.StoryContextInstance.SaveChanges(); return(StoryInstance); } catch (Exception ex) { Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++"); Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++"); Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++"); Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++"); if (ex is CustomHttpResponseException) { CustomHttpResponseException cex = ex as CustomHttpResponseException; Console.WriteLine(JsonConvert.SerializeObject(cex.CustomData)); } else { Console.WriteLine(JsonConvert.SerializeObject(ex)); } throw ex; } }
public async Task <IActionResult> Login(LoginModel LoginInstance) { try { var key = "a00bc" + DateTime.Now.Ticks.ToString() + "ztmmn01" + DateTime.Now.Ticks.ToString(); AuthModel AuthInstance = await this.AuthServiceInstance.Login(LoginInstance); UserAggregate User = new UserAggregate(); User.AccessToken = AuthInstance.AccessToken; User UserInstance = await this.AccountServiceInstance.Detail("Bearer " + AuthInstance.AccessToken); User.UserInfo = UserInstance; string UserString = ""; var Serialize = new JsonSerializer(); using (StringWriter Writer = new Utf8StringWriter()) { Serialize.Serialize(Writer, User); UserString = Writer.ToString(); } Console.WriteLine(UserString); TempData.Add(key, UserString); return(RedirectToAction("Passport", new { k = key })); } catch (Exception ex) { ErrorView Error = new ErrorView(); Error.HasError = true; if (ex is CustomHttpResponseException) { CustomHttpResponseException cex = ex as CustomHttpResponseException; Console.WriteLine("======================================="); Console.WriteLine(JsonConvert.SerializeObject(cex.CustomData)); if (cex.CustomData.StatusCode == 400) { Error.Message = "Tên đăng nhập hoặc mất khẩu sai"; } else { Error.Message = "Đã có lỗi xảy ra"; } } else { Console.WriteLine("======================================="); Console.WriteLine(JsonConvert.SerializeObject(ex)); Error.Message = "Đã có lỗi xảy ra"; } ViewBag.Error = Error; return(View(LoginInstance)); } }
public async Task <IActionResult> Create(CreateUser User) { try { var Token = HttpContext.Request.Headers["Token"]; User UserInstance = await this.AccountServiceInstance.Create(User, Token); return(RedirectToAction("Detail", new { Id = UserInstance.Id })); } catch (Exception ex) { if (ex is CustomHttpResponseException) { CustomHttpResponseException cex = ex as CustomHttpResponseException; Console.WriteLine("------------------------------------------------"); Console.WriteLine("------------------------------------------------"); Console.WriteLine("------------------------------------------------"); Console.WriteLine(JsonConvert.SerializeObject(cex.CustomData)); } throw ex; } }
public async Task <StoryAggregate> Create(string Token, StoryAggregate StoryInstance) { try { var Handler = new JwtSecurityTokenHandler(); Token = Token.Replace("Bearer", "").Trim(); var JsonToken = Handler.ReadJwtToken(Token); string Id = JsonToken.Claims.First(c => c.Type == "id").Value; User User = this.StoryContextInstance.Users.SingleOrDefault(u => u.Id == Id); if (User == null) { User = await this.AccountServiceInstance.Detail("Bearer " + Token); this.StoryContextInstance.Users.Add(User); this.StoryContextInstance.SaveChanges(); } Story Story = StoryInstance.StoryInfo; Story.UserId = Id; Story.User = User; Story.Category = this.StoryContextInstance.Categories.SingleOrDefault(c => c.Id == Story.CategoryId); this.StoryContextInstance.Stories.Add(Story); List <Tag> Tags = new List <Tag>(); Tags.AddRange(StoryInstance.Tags); Tags.ForEach(Tag => { StoryTag StoryTag = new StoryTag(); StoryTag.TagId = Tag.Id; StoryTag.StoryId = Story.Id; this.StoryContextInstance.StoryTags.Add(StoryTag); }); this.StoryContextInstance.SaveChanges(); return(StoryInstance); } catch (Exception ex) { Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++"); Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++"); Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++"); Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++"); if (ex is CustomHttpResponseException) { CustomHttpResponseException cex = ex as CustomHttpResponseException; Console.WriteLine(JsonConvert.SerializeObject(cex.CustomData)); } else { Console.WriteLine(JsonConvert.SerializeObject(ex)); } throw ex; } }