http service: optimize session queries

This commit is contained in:
Adam
2018-12-08 21:25:49 -05:00
parent c5bb84a57b
commit 5e25397fc6
2 changed files with 13 additions and 13 deletions

View File

@@ -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();
}

View File

@@ -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();
}
}