public async Task <IActionResult> PutManga(int id, Books mangas) { if (id != mangas.Id) { return(BadRequest()); } _context.Entry(mangas).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!MangaExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <UserWithTocken> > Login([FromBody] Users user) { var userdb = await _context.Users.Include("RefreshToken").Where(u => u.Login == user.Login).FirstOrDefaultAsync(); if (userdb == null || !_security.CheckPassword(user.Password, userdb.Password)) { return(BadRequest("Wrong password or login")); } //refresh token foreach (RefreshToken token in userdb.RefreshToken.ToList()) { if (token.ExpiryDate < DateTime.Now) { userdb.RefreshToken.Remove(token); } } RefreshToken refreshToken = userdb.RefreshToken.OrderBy(token => token.ExpiryDate).FirstOrDefault(); if (userdb.RefreshToken.Count == 0) { refreshToken = GenerateRefreshToken(); userdb.RefreshToken.Add(refreshToken); } else { if (refreshToken.ExpiryDate < DateTime.Now.AddDays(1)) { userdb.RefreshToken.Remove(refreshToken); refreshToken = GenerateRefreshToken(); userdb.RefreshToken.Add(refreshToken); } } await _context.SaveChangesAsync(); UserWithTocken result = new UserWithTocken(userdb); result.Token = GenerateJWTToken(userdb.Id, userdb.Roles); result.RefreshToken_ = refreshToken.Token; return(Ok(result)); }