http service: optimize session queries
This commit is contained in:
@@ -64,26 +64,24 @@ public class SessionController
|
||||
@RequestMapping("/ping")
|
||||
public ResponseEntity ping(@RequestParam("session") UUID uuid)
|
||||
{
|
||||
SessionEntry sessionEntry = sessionService.findSessionByUUID(uuid);
|
||||
if (sessionEntry == null)
|
||||
int updated = sessionService.updateLast(uuid);
|
||||
if (updated == 0)
|
||||
{
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
sessionService.updateLast(uuid);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
public ResponseEntity delete(@RequestParam("session") UUID uuid, HttpServletRequest request)
|
||||
{
|
||||
SessionEntry sessionEntry = sessionService.findSessionByUUID(uuid);
|
||||
if (sessionEntry == null)
|
||||
int deleted = sessionService.deleteSession(uuid);
|
||||
if (deleted == 0)
|
||||
{
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
sessionService.deleteSession(sessionEntry);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
|
||||
@@ -70,25 +70,27 @@ public class SessionService
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteSession(SessionEntry session)
|
||||
public int deleteSession(UUID id)
|
||||
{
|
||||
try (Connection con = sql2o.open())
|
||||
{
|
||||
con.createQuery("delete from session where uuid = :uuid")
|
||||
.addParameter("uuid", session.getUuid().toString())
|
||||
.executeUpdate();
|
||||
return con.createQuery("delete from session where uuid = :uuid")
|
||||
.addParameter("uuid", id.toString())
|
||||
.executeUpdate()
|
||||
.getResult();
|
||||
}
|
||||
}
|
||||
|
||||
public void updateLast(UUID session)
|
||||
public int updateLast(UUID session)
|
||||
{
|
||||
try (Connection con = sql2o.open())
|
||||
{
|
||||
Instant last = Instant.now();
|
||||
con.createQuery("update session set last = :last where uuid = :uuid")
|
||||
return con.createQuery("update session set last = :last where uuid = :uuid")
|
||||
.addParameter("last", last)
|
||||
.addParameter("uuid", session.toString())
|
||||
.executeUpdate();
|
||||
.executeUpdate()
|
||||
.getResult();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user