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-06 11:59:23 -06:00
import type { NextApiRequest , NextApiResponse } from "next" ;
import { getServerSession } from "next-auth/next" ;
import { authOptions } from "pages/api/auth/[...nextauth]" ;
2023-02-18 21:32:02 -06:00
import getCollections from "@/lib/api/controllers/collections/getCollections" ;
2023-02-24 11:32:28 -06:00
import postCollection from "@/lib/api/controllers/collections/postCollection" ;
2023-04-28 16:10:29 -05:00
import updateCollection from "@/lib/api/controllers/collections/updateCollection" ;
import deleteCollection from "@/lib/api/controllers/collections/deleteCollection" ;
2023-02-06 11:59:23 -06:00
2023-03-28 02:31:50 -05:00
export default async function ( req : NextApiRequest , res : NextApiResponse ) {
2023-02-06 11:59:23 -06:00
const session = await getServerSession ( req , res , authOptions ) ;
2023-02-08 15:11:33 -06:00
if ( ! session ? . user ? . email ) {
return res . status ( 401 ) . json ( { response : "You must be logged in." } ) ;
2023-02-06 11:59:23 -06:00
}
2023-03-28 02:31:50 -05:00
if ( req . method === "GET" ) {
const collections = await getCollections ( session . user . id ) ;
return res
. status ( collections . status )
. json ( { response : collections.response } ) ;
} else if ( req . method === "POST" ) {
2023-04-24 16:30:40 -05:00
const newCollection = await postCollection ( req . body , session . user . id ) ;
2023-03-28 02:31:50 -05:00
return res
. status ( newCollection . status )
. json ( { response : newCollection.response } ) ;
2023-04-28 16:10:29 -05:00
} else if ( req . method === "PUT" ) {
const updated = await updateCollection ( req . body , session . user . id ) ;
return res . status ( updated . status ) . json ( { response : updated.response } ) ;
} else if ( req . method === "DELETE" ) {
const deleted = await deleteCollection ( req . body , session . user . id ) ;
return res . status ( deleted . status ) . json ( { response : deleted.response } ) ;
2023-03-28 02:31:50 -05:00
}
2023-02-06 11:59:23 -06:00
}