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") @RequestMapping("/ping")
public ResponseEntity ping(@RequestParam("session") UUID uuid) public ResponseEntity ping(@RequestParam("session") UUID uuid)
{ {
SessionEntry sessionEntry = sessionService.findSessionByUUID(uuid); int updated = sessionService.updateLast(uuid);
if (sessionEntry == null) if (updated == 0)
{ {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }
sessionService.updateLast(uuid);
return ResponseEntity.ok().build(); return ResponseEntity.ok().build();
} }
@DeleteMapping @DeleteMapping
public ResponseEntity delete(@RequestParam("session") UUID uuid, HttpServletRequest request) public ResponseEntity delete(@RequestParam("session") UUID uuid, HttpServletRequest request)
{ {
SessionEntry sessionEntry = sessionService.findSessionByUUID(uuid); int deleted = sessionService.deleteSession(uuid);
if (sessionEntry == null) if (deleted == 0)
{ {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }
sessionService.deleteSession(sessionEntry);
return ResponseEntity.ok().build(); 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()) try (Connection con = sql2o.open())
{ {
con.createQuery("delete from session where uuid = :uuid") return con.createQuery("delete from session where uuid = :uuid")
.addParameter("uuid", session.getUuid().toString()) .addParameter("uuid", id.toString())
.executeUpdate(); .executeUpdate()
.getResult();
} }
} }
public void updateLast(UUID session) public int updateLast(UUID session)
{ {
try (Connection con = sql2o.open()) try (Connection con = sql2o.open())
{ {
Instant last = Instant.now(); 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("last", last)
.addParameter("uuid", session.toString()) .addParameter("uuid", session.toString())
.executeUpdate(); .executeUpdate()
.getResult();
} }
} }