{"size": 2501, "ext": "js", "max_stars_repo_path": "server/controllers/postsController.js", "max_stars_repo_name": "Buzzaw91/BlogStack", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/controllers/postsController.js", "max_issues_repo_name": "Buzzaw91/BlogStack", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/controllers/postsController.js", "max_forks_repo_name": "Buzzaw91/BlogStack", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 29.7738095238, "max_line_length": 241, "alphanum_fraction": 0.6149540184}
const asyncHandler = require('express-async-handler')
const slugify = require('slugify')
const db = require('../db/index.js');
const { counter } = require('../utils/slugCounter')
const toCamelCase = require('../utils/camelCaser')

// @desc    Get all posts
// @route   GET /api/v1/posts
// @access  Private/Admin

const getAllPosts = asyncHandler( async (req, res) => {

    try {
        const { rows } = await db.query('SELECT * FROM posts;');

        return res.status(200).json({...rows})
    } catch(err) {
        console.error(err)
        res.status(400).json({error: err})
    }


})

// @desc    Create a post
// @route   POST /api/v1/posts
// @access  Private

const createPost = asyncHandler ( async (req, res) => {

    const { title, metaTitle, published, id, url, body } = req.body;
    let slug = slugify(title);
    slug = await counter(slug);

    const { rows } = await db.query(`INSERT INTO posts (slug, title, meta_title, published, url, user_id, body)
    VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *
    `,[slug, title, metaTitle, published, url, id, body]);
    if (rows[0]) {
        res.status(201).json({...rows[0]})
    } else {
        res.status(500).json({error: 'Something went wrong on the server...'})
    }


})

// @desc    Get all PUBLISHED post  by a user
// @route   GET /api/v1/posts/:id
// @access  Public

const getPostsByUser = asyncHandler( async (req, res) => {
    const { id } = req.params;

    try {
        const { rows } = await db.query('SELECT * FROM posts WHERE user_id = $1 AND published = true', [id]);
        return res.status(200).json(rows);
    } catch(err) {
        console.log(err.stack);
        res.status(500).json({error: 'Something went wrong on the server...'})
    }


})

// @desc    Get single PUBLISHED post  by a user
// @route   GET /api/v1/posts/:id
// @access  Public

const getPostByUser = asyncHandler( async (req, res) => {
    const { slug } = req.params;

    try {
        const { rows } = await db.query('SELECT title, slug, meta_title, published, posts.created_at, posts.updated_at, published_at, url, body, sub_title, username FROM posts JOIN users ON posts.user_id = users.id WHERE slug = $1', [slug]);
        const result = toCamelCase(rows)
        return res.status(200).json(result[0]);
    } catch(err) {
        console.log(err.stack);
        res.status(500).json({error: 'Something went wrong on the server...'})
    }


})

module.exports = { getAllPosts, createPost, getPostsByUser, getPostByUser }