11 changed files with 117 additions and 3 deletions
@ -1,7 +1,26 @@
@@ -1,7 +1,26 @@
|
||||
<script> |
||||
import { t } from '../../translations.svelte.js'; |
||||
import { onMount } from 'svelte'; |
||||
|
||||
let items = $state(null); |
||||
let error = $state(null); |
||||
async function loadItems(){ |
||||
const url = `${location.protocol}//${location.host.replace('5173','8080')}/api/items/list`; |
||||
const resp = await fetch(url,{credentials:'include'}); |
||||
if (resp.ok){ |
||||
items = await resp.json(); |
||||
} else { |
||||
error = await resp.body(); |
||||
} |
||||
} |
||||
|
||||
onMount(loadItems); |
||||
|
||||
</script> |
||||
|
||||
<div> |
||||
<h1>Items</h1> |
||||
{#if error} |
||||
<span class="error">{error}</span> |
||||
{/if} |
||||
</div> |
||||
@ -0,0 +1,8 @@
@@ -0,0 +1,8 @@
|
||||
description = "Umbrella : Items" |
||||
|
||||
dependencies{ |
||||
implementation(project(":core")) |
||||
implementation("de.srsoftware:configuration.api:1.0.2") |
||||
implementation("de.srsoftware:tools.jdbc:1.3.2") |
||||
implementation("de.srsoftware:tools.util:2.0.3") |
||||
} |
||||
@ -0,0 +1,7 @@
@@ -0,0 +1,7 @@
|
||||
package de.srsoftware.umbrella.items; |
||||
|
||||
public class Constants { |
||||
private Constants(){} |
||||
|
||||
public static final String CONFIG_DATABASE = "umbrella.modules.items.database"; |
||||
} |
||||
@ -0,0 +1,4 @@
@@ -0,0 +1,4 @@
|
||||
package de.srsoftware.umbrella.items; |
||||
|
||||
public class Item { |
||||
} |
||||
@ -0,0 +1,53 @@
@@ -0,0 +1,53 @@
|
||||
package de.srsoftware.umbrella.items; |
||||
|
||||
import com.sun.net.httpserver.HttpExchange; |
||||
import de.srsoftware.configuration.Configuration; |
||||
import de.srsoftware.tools.Path; |
||||
import de.srsoftware.tools.SessionToken; |
||||
import de.srsoftware.umbrella.core.BaseHandler; |
||||
import de.srsoftware.umbrella.core.api.UserService; |
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException; |
||||
import de.srsoftware.umbrella.core.model.Token; |
||||
import de.srsoftware.umbrella.core.model.UmbrellaUser; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.Optional; |
||||
|
||||
import static de.srsoftware.umbrella.core.ConnectionProvider.connect; |
||||
import static de.srsoftware.umbrella.core.Paths.LIST; |
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException; |
||||
import static de.srsoftware.umbrella.items.Constants.CONFIG_DATABASE; |
||||
|
||||
public class ItemApi extends BaseHandler { |
||||
|
||||
private final ItemDb itemDb; |
||||
private final UserService users; |
||||
|
||||
public ItemApi(Configuration config, UserService userService) throws UmbrellaException { |
||||
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingFieldException(CONFIG_DATABASE)); |
||||
itemDb = new SqliteDb(connect(dbFile)); |
||||
users = userService; |
||||
} |
||||
|
||||
@Override |
||||
public boolean doGet(Path path, HttpExchange ex) throws IOException { |
||||
addCors(ex); |
||||
try { |
||||
Optional<Token> token = SessionToken.from(ex).map(Token::of); |
||||
var user = users.loadUser(token); |
||||
if (user.isEmpty()) return unauthorized(ex); |
||||
var head = path.pop(); |
||||
return switch (head) { |
||||
case LIST -> listItems(ex,user); |
||||
default -> super.doGet(path,ex); |
||||
}; |
||||
} catch (UmbrellaException e){ |
||||
return send(ex,e); |
||||
} |
||||
} |
||||
|
||||
private boolean listItems(HttpExchange ex, Optional<UmbrellaUser> user) throws IOException { |
||||
var items = itemDb.list(); |
||||
return notImplemented(ex,"listItems",this); |
||||
} |
||||
} |
||||
@ -0,0 +1,7 @@
@@ -0,0 +1,7 @@
|
||||
package de.srsoftware.umbrella.items; |
||||
|
||||
import java.util.Collection; |
||||
|
||||
public interface ItemDb { |
||||
Collection<Item> list(); |
||||
} |
||||
@ -0,0 +1,11 @@
@@ -0,0 +1,11 @@
|
||||
package de.srsoftware.umbrella.items; |
||||
|
||||
import java.sql.Connection; |
||||
|
||||
public class SqliteDb implements ItemDb{ |
||||
private final Connection db; |
||||
|
||||
public SqliteDb(Connection connection) { |
||||
db = connection; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue