2023-04-23 08:26:39 -05:00
// Copyright (C) 2022-present Daniel31x13 <daniel31x13@gmail.com>
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3.
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
2023-02-24 11:32:28 -06:00
import type { NextApiRequest , NextApiResponse } from "next" ;
import { getServerSession } from "next-auth/next" ;
import { authOptions } from "pages/api/auth/[...nextauth]" ;
2023-03-05 15:03:20 -06:00
import getLinks from "@/lib/api/controllers/links/getLinks" ;
2023-02-24 11:32:28 -06:00
import postLink from "@/lib/api/controllers/links/postLink" ;
2023-03-23 10:25:17 -05:00
import deleteLink from "@/lib/api/controllers/links/deleteLink" ;
2023-03-28 10:11:34 -05:00
import updateLink from "@/lib/api/controllers/links/updateLink" ;
2023-02-24 11:32:28 -06:00
2023-03-28 02:31:50 -05:00
export default async function ( req : NextApiRequest , res : NextApiResponse ) {
2023-02-24 11:32:28 -06:00
const session = await getServerSession ( req , res , authOptions ) ;
if ( ! session ? . user ? . email ) {
return res . status ( 401 ) . json ( { response : "You must be logged in." } ) ;
}
2023-03-28 02:31:50 -05:00
if ( req . method === "GET" ) {
const links = await getLinks ( session . user . id ) ;
return res . status ( links . status ) . json ( { response : links.response } ) ;
} else if ( req . method === "POST" ) {
const newlink = await postLink ( req . body , session . user . id ) ;
return res . status ( newlink . status ) . json ( {
response : newlink.response ,
} ) ;
2023-03-28 10:11:34 -05:00
} else if ( req . method === "PUT" ) {
const updated = await updateLink ( req . body , session . user . id ) ;
return res . status ( updated . status ) . json ( {
response : updated.response ,
} ) ;
2023-04-28 16:10:29 -05:00
} else if ( req . method === "DELETE" ) {
const deleted = await deleteLink ( req . body , session . user . id ) ;
return res . status ( deleted . status ) . json ( {
response : deleted.response ,
} ) ;
2023-03-28 02:31:50 -05:00
}
2023-02-24 11:32:28 -06:00
}