Skip to content

Commit 4066204

Browse files
committed
Basic for sale system implementation
1 parent 5626bb3 commit 4066204

7 files changed

Lines changed: 99 additions & 49 deletions

File tree

Site/src/routes/(main)/catalog/[id=asset]/[name]/+page.server.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type Asset = {
3737
updated: Date
3838
}
3939
forSale: boolean
40+
isCreator: boolean
4041
name: string
4142
owned: boolean
4243
price: number
@@ -168,6 +169,7 @@ actions.buy = async e => {
168169
id: string
169170
username: string
170171
}
172+
forSale: boolean
171173
name: string
172174
owned: boolean
173175
price: number
@@ -181,6 +183,7 @@ actions.buy = async e => {
181183
if (asset.owned) error(400, "You already own this item")
182184
if (asset.visibility !== "Visible")
183185
error(400, "This item hasn't been approved yet")
186+
if (!asset.forSale) error(400, "This item is not for sale")
184187

185188
if (asset.price > 0) {
186189
// todo work out how free assets are supposed to work

Site/src/routes/(main)/catalog/[id=asset]/[name]/+page.svelte

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@
5858
<h1>{data.asset.name}</h1>
5959

6060
<div class="flex flex-col items-center">
61-
{#if data.asset.creator.username === user.username || user.permissionLevel >= 4}
61+
{#if data.asset.isCreator || user.permissionLevel >= 4}
6262
<div>
6363
<a
6464
aria-label="Asset settings"
65-
href="/catalog/{data.asset.id}/{data.slug}/settings"
65+
href="/catalog/{data.asset
66+
.id}/{data.slug}/settings"
6667
class="btn btn-sm btn-secondary">
6768
<fa fa-sliders></fa>
6869
</a>
@@ -80,7 +81,9 @@
8081
action="?/rerender">
8182
<button
8283
class="btn accent-text pl-4 pr-0 text-start">
83-
<fa fa-arrows-rotate class="pr-2">
84+
<fa
85+
fa-arrows-rotate
86+
class="pr-2">
8487
</fa>
8588
<b>Rerender</b>
8689
</button>
@@ -125,42 +128,46 @@
125128
</p>
126129
</div>
127130
<div class="w-full md:w-2/3 flex flex-row-reverse">
128-
<div class="card light-text p-4">
129-
{#if itsFree}
130-
<b class="pb-2">Free</b>
131-
{:else}
132-
<table>
133-
<tbody>
134-
<tr>
135-
<td>Price</td>
136-
<td class="text-emerald-600">
137-
{data.currencySymbol}
138-
{data.asset.price}
139-
</td>
140-
</tr>
141-
<tr>
142-
<td>Fee</td>
143-
<td class="text-yellow-500">
144-
{data.currencySymbol}
145-
{fee}
146-
</td>
147-
</tr>
148-
</tbody>
149-
</table>
150-
{/if}
151-
{#if !data.asset.owned}
152-
<button popovertarget="buy" class="btn btn-success">
153-
<strong class="text-xl">
154-
{itsFree ? "Get" : "Buy now"}
131+
{#if data.asset.forSale}
132+
<div class="card light-text p-4">
133+
{#if itsFree}
134+
<b class="pb-2">Free</b>
135+
{:else}
136+
<table>
137+
<tbody>
138+
<tr>
139+
<td>Price</td>
140+
<td class="text-emerald-600">
141+
{data.currencySymbol}
142+
{data.asset.price}
143+
</td>
144+
</tr>
145+
<tr>
146+
<td>Fee</td>
147+
<td class="text-yellow-500">
148+
{data.currencySymbol}
149+
{fee}
150+
</td>
151+
</tr>
152+
</tbody>
153+
</table>
154+
{/if}
155+
{#if !data.asset.owned}
156+
<button
157+
popovertarget="buy"
158+
class="btn btn-success">
159+
<strong class="text-xl">
160+
{itsFree ? "Get" : "Buy now"}
161+
</strong>
162+
</button>
163+
{:else}
164+
<strong
165+
class="btn btn-dark bg-a3 pointer-events-none text-xl">
166+
Owned
155167
</strong>
156-
</button>
157-
{:else}
158-
<strong
159-
class="btn btn-dark bg-a3 pointer-events-none text-xl">
160-
Owned
161-
</strong>
162-
{/if}
163-
</div>
168+
{/if}
169+
</div>
170+
{/if}
164171
</div>
165172
</div>
166173
</div>

Site/src/routes/(main)/catalog/[id=asset]/[name]/asset.surql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ SELECT
22
*,
33
record::id(id) AS id,
44
(SELECT status, username FROM <-created<-user)[0] AS creator,
5+
(SELECT VALUE id FROM <-created<-user)[0] = $user AS isCreator,
56
count(<-ownsAsset<-user) AS sold,
67
$user IN <-ownsAsset<-user.id AS owned,
78

Site/src/routes/(main)/catalog/[id=asset]/[name]/settings/+page.server.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import formError from "$lib/server/formError"
77
import { db, Record } from "$lib/server/surreal"
88
import { encode } from "$lib/urlName"
99
import assetQuery from "./asset.surql"
10-
import assetCheckQuery from "./asset.surql"
10+
import assetCheckQuery from "./assetCheck.surql"
11+
import updateAssetQuery from "./updateAsset.surql"
1112

1213
type Asset = {
1314
id: string
@@ -28,6 +29,9 @@ const schema = type({
2829
// This is how I show my love
2930
// I made it in my mind because
3031
// I blame it on my ADD, baby
32+
name: "3 <= string <= 50",
33+
description: "(0 <= string <= 1000) | undefined",
34+
price: "0 <= number.integer <= 999",
3135
forSale: "boolean",
3236
})
3337

@@ -40,13 +44,17 @@ export async function load({ locals, params }) {
4044
})
4145
if (!asset) error(404, "Not Found")
4246

47+
console.log(asset)
48+
4349
return {
4450
...asset,
4551
slug: encode(asset.name),
4652
form: await superValidate(
4753
{
4854
name: asset.name,
4955
forSale: asset.forSale,
56+
description: asset.description.text,
57+
price: asset.price,
5058
},
5159
arktype(schema)
5260
),
@@ -58,7 +66,7 @@ export const actions: import("./$types").Actions = {}
5866
type AssetCheck = {
5967
id: string
6068
created: Date
61-
creatorId: string
69+
isCreator: boolean
6270
description: {
6371
text: string
6472
updated: Date
@@ -81,10 +89,13 @@ actions.default = async ({ locals, params, request }) => {
8189
user: Record("user", user.id),
8290
})
8391
if (!asset) error(404, "Not Found")
84-
if (asset.creatorId !== user.id || user.permissionLevel < 4)
92+
if (!asset.isCreator && user.permissionLevel < 4)
8593
error(403, "You do not have permission to edit this asset")
8694

87-
await db.merge(Record("asset", id), form.data)
95+
await db.query(updateAssetQuery, {
96+
asset: Record("asset", id),
97+
...form.data,
98+
})
8899

89100
return message(form, "Asset data updated successfully!")
90101
}

Site/src/routes/(main)/catalog/[id=asset]/[name]/settings/+page.svelte

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
<script lang="ts">
2+
import { get } from "svelte/store"
23
import { superForm } from "sveltekit-superforms/client"
34
import Form from "$components/forms/Form.svelte"
45
import Input from "$components/forms/Input.svelte"
56
import Textarea from "$components/forms/Textarea.svelte"
67
import Head from "$components/Head.svelte"
7-
8+
89
const { data } = $props()
910
1011
let formData = $derived(superForm(data.form))
1112
let { form } = $derived(formData)
13+
14+
$effect(() => {
15+
if (data.description && !get(formData.form).description)
16+
$form.description = data.description.text
17+
})
1218
</script>
1319

1420
<Head name={data.siteName} title="{data.name} Settings" />
@@ -26,12 +32,17 @@
2632
{formData}
2733
enctype="multipart/form-data"
2834
submit=" <fa fa-save></fa> Save changes">
29-
<Input {formData} name="name" label="Name" />
30-
3135
<Input
3236
{formData}
33-
type="checkbox"
34-
name="forSale"
35-
label="For sale" />
37+
name="name"
38+
label="Name"
39+
placeholder="Make sure to make it accurate" />
40+
<Textarea
41+
{formData}
42+
name="description"
43+
label="Description"
44+
placeholder="Up to 1000 characters" />
45+
<Input {formData} type="number" name="price" label="Price" />
46+
<Input {formData} type="checkbox" name="forSale" label="For sale" />
3647
</Form>
3748
</div>

Site/src/routes/(main)/catalog/[id=asset]/[name]/settings/assetCheck.surql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
SELECT
22
*,
33
record::id(id) AS id,
4-
SELECT VALUE record::id(id) AS id FROM <-created<-user AS creatorId,
4+
(SELECT VALUE id FROM <-created<-user)[0] = $user AS isCreator,
55

66
(SELECT text, updated FROM $parent.description
77
ORDER BY updated)[$] AS description
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
LET $og = SELECT
2+
(SELECT text, updated FROM $parent.description
3+
ORDER BY updated)[$] AS description
4+
FROM $asset;
5+
6+
UPDATE $asset MERGE {
7+
name: $name,
8+
forSale: $forSale,
9+
price: $price,
10+
};
11+
12+
IF $og.description.text != $description {
13+
UPDATE $asset SET description += {
14+
text: $description,
15+
updated: time::now(),
16+
};
17+
};

0 commit comments

Comments
 (0)