mirror of
https://github.com/umami-software/umami.git
synced 2026-02-06 21:57:16 +01:00
Link editing.
This commit is contained in:
parent
0558563d35
commit
5f4b83b09c
13 changed files with 123 additions and 89 deletions
|
|
@ -1,39 +1,32 @@
|
|||
import { z } from 'zod';
|
||||
import { canUpdateLink, canDeleteLink, canViewLink } from '@/validations';
|
||||
import { SHARE_ID_REGEX } from '@/lib/constants';
|
||||
import { parseRequest } from '@/lib/request';
|
||||
import { ok, json, unauthorized, serverError } from '@/lib/response';
|
||||
import { ok, json, unauthorized, serverError, badRequest } from '@/lib/response';
|
||||
import { deleteLink, getLink, updateLink } from '@/queries';
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ websiteId: string }> },
|
||||
) {
|
||||
export async function GET(request: Request, { params }: { params: Promise<{ linkId: string }> }) {
|
||||
const { auth, error } = await parseRequest(request);
|
||||
|
||||
if (error) {
|
||||
return error();
|
||||
}
|
||||
|
||||
const { websiteId } = await params;
|
||||
const { linkId } = await params;
|
||||
|
||||
if (!(await canViewLink(auth, websiteId))) {
|
||||
if (!(await canViewLink(auth, linkId))) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
const website = await getLink(websiteId);
|
||||
const website = await getLink(linkId);
|
||||
|
||||
return json(website);
|
||||
}
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ websiteId: string }> },
|
||||
) {
|
||||
export async function POST(request: Request, { params }: { params: Promise<{ linkId: string }> }) {
|
||||
const schema = z.object({
|
||||
name: z.string().optional(),
|
||||
domain: z.string().optional(),
|
||||
shareId: z.string().regex(SHARE_ID_REGEX).nullable().optional(),
|
||||
name: z.string(),
|
||||
url: z.string(),
|
||||
slug: z.string(),
|
||||
});
|
||||
|
||||
const { auth, body, error } = await parseRequest(request, schema);
|
||||
|
|
@ -42,20 +35,20 @@ export async function POST(
|
|||
return error();
|
||||
}
|
||||
|
||||
const { websiteId } = await params;
|
||||
const { name, domain, shareId } = body;
|
||||
const { linkId } = await params;
|
||||
const { name, url, slug } = body;
|
||||
|
||||
if (!(await canUpdateLink(auth, websiteId))) {
|
||||
if (!(await canUpdateLink(auth, linkId))) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
try {
|
||||
const website = await updateLink(websiteId, { name, domain, shareId });
|
||||
const result = await updateLink(linkId, { name, url, slug });
|
||||
|
||||
return Response.json(website);
|
||||
return Response.json(result);
|
||||
} catch (e: any) {
|
||||
if (e.message.includes('Unique constraint') && e.message.includes('share_id')) {
|
||||
return serverError(new Error('That share ID is already taken.'));
|
||||
if (e.message.includes('Unique constraint') && e.message.includes('slug')) {
|
||||
return badRequest('That slug is already taken.');
|
||||
}
|
||||
|
||||
return serverError(e);
|
||||
|
|
@ -64,7 +57,7 @@ export async function POST(
|
|||
|
||||
export async function DELETE(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ websiteId: string }> },
|
||||
{ params }: { params: Promise<{ linkId: string }> },
|
||||
) {
|
||||
const { auth, error } = await parseRequest(request);
|
||||
|
||||
|
|
@ -72,13 +65,13 @@ export async function DELETE(
|
|||
return error();
|
||||
}
|
||||
|
||||
const { websiteId } = await params;
|
||||
const { linkId } = await params;
|
||||
|
||||
if (!(await canDeleteLink(auth, websiteId))) {
|
||||
if (!(await canDeleteLink(auth, linkId))) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
await deleteLink(websiteId);
|
||||
await deleteLink(linkId);
|
||||
|
||||
return ok();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,39 +1,32 @@
|
|||
import { z } from 'zod';
|
||||
import { canUpdateWebsite, canDeleteWebsite, canViewWebsite } from '@/validations';
|
||||
import { SHARE_ID_REGEX } from '@/lib/constants';
|
||||
import { canUpdatePixel, canDeletePixel, canViewPixel } from '@/validations';
|
||||
import { parseRequest } from '@/lib/request';
|
||||
import { ok, json, unauthorized, serverError } from '@/lib/response';
|
||||
import { deleteWebsite, getWebsite, updateWebsite } from '@/queries';
|
||||
import { ok, json, unauthorized, serverError, badRequest } from '@/lib/response';
|
||||
import { deletePixel, getPixel, updatePixel } from '@/queries';
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ websiteId: string }> },
|
||||
) {
|
||||
export async function GET(request: Request, { params }: { params: Promise<{ pixelId: string }> }) {
|
||||
const { auth, error } = await parseRequest(request);
|
||||
|
||||
if (error) {
|
||||
return error();
|
||||
}
|
||||
|
||||
const { websiteId } = await params;
|
||||
const { pixelId } = await params;
|
||||
|
||||
if (!(await canViewWebsite(auth, websiteId))) {
|
||||
if (!(await canViewPixel(auth, pixelId))) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
const website = await getWebsite(websiteId);
|
||||
const pixel = await getPixel(pixelId);
|
||||
|
||||
return json(website);
|
||||
return json(pixel);
|
||||
}
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ websiteId: string }> },
|
||||
) {
|
||||
export async function POST(request: Request, { params }: { params: Promise<{ pixelId: string }> }) {
|
||||
const schema = z.object({
|
||||
name: z.string().optional(),
|
||||
domain: z.string().optional(),
|
||||
shareId: z.string().regex(SHARE_ID_REGEX).nullable().optional(),
|
||||
name: z.string(),
|
||||
url: z.string().url(),
|
||||
slug: z.string().min(8),
|
||||
});
|
||||
|
||||
const { auth, body, error } = await parseRequest(request, schema);
|
||||
|
|
@ -42,20 +35,20 @@ export async function POST(
|
|||
return error();
|
||||
}
|
||||
|
||||
const { websiteId } = await params;
|
||||
const { pixelId } = await params;
|
||||
const { name, domain, shareId } = body;
|
||||
|
||||
if (!(await canUpdateWebsite(auth, websiteId))) {
|
||||
if (!(await canUpdatePixel(auth, pixelId))) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
try {
|
||||
const website = await updateWebsite(websiteId, { name, domain, shareId });
|
||||
const pixel = await updatePixel(pixelId, { name, domain, shareId });
|
||||
|
||||
return Response.json(website);
|
||||
return Response.json(pixel);
|
||||
} catch (e: any) {
|
||||
if (e.message.includes('Unique constraint') && e.message.includes('share_id')) {
|
||||
return serverError(new Error('That share ID is already taken.'));
|
||||
if (e.message.includes('Unique constraint') && e.message.includes('slug')) {
|
||||
return badRequest('That slug is already taken.');
|
||||
}
|
||||
|
||||
return serverError(e);
|
||||
|
|
@ -64,7 +57,7 @@ export async function POST(
|
|||
|
||||
export async function DELETE(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ websiteId: string }> },
|
||||
{ params }: { params: Promise<{ pixelId: string }> },
|
||||
) {
|
||||
const { auth, error } = await parseRequest(request);
|
||||
|
||||
|
|
@ -72,13 +65,13 @@ export async function DELETE(
|
|||
return error();
|
||||
}
|
||||
|
||||
const { websiteId } = await params;
|
||||
const { pixelId } = await params;
|
||||
|
||||
if (!(await canDeleteWebsite(auth, websiteId))) {
|
||||
if (!(await canDeletePixel(auth, pixelId))) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
await deleteWebsite(websiteId);
|
||||
await deletePixel(pixelId);
|
||||
|
||||
return ok();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,9 +20,9 @@ export async function GET(request: Request) {
|
|||
|
||||
const filters = await getQueryFilters(query);
|
||||
|
||||
const inks = await getUserLinks(auth.user.id, filters);
|
||||
const links = await getUserLinks(auth.user.id, filters);
|
||||
|
||||
return json(inks);
|
||||
return json(links);
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import { z } from 'zod';
|
|||
import { canUpdateWebsite, canDeleteWebsite, canViewWebsite } from '@/validations';
|
||||
import { SHARE_ID_REGEX } from '@/lib/constants';
|
||||
import { parseRequest } from '@/lib/request';
|
||||
import { ok, json, unauthorized, serverError } from '@/lib/response';
|
||||
import { ok, json, unauthorized, serverError, badRequest } from '@/lib/response';
|
||||
import { deleteWebsite, getWebsite, updateWebsite } from '@/queries';
|
||||
|
||||
export async function GET(
|
||||
|
|
@ -55,7 +55,7 @@ export async function POST(
|
|||
return Response.json(website);
|
||||
} catch (e: any) {
|
||||
if (e.message.includes('Unique constraint') && e.message.includes('share_id')) {
|
||||
return serverError(new Error('That share ID is already taken.'));
|
||||
return badRequest('That share ID is already taken.');
|
||||
}
|
||||
|
||||
return serverError(e);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue