Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

save #65

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.env
.git
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PORT=
DB_HOST=
SECRET_KEY=
GMAIL_EMAIL=
GMAIL_PASSWORD=
13 changes: 13 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
env: {
commonjs: true,
es2021: true,
node: true,
jest: true,
},
extends: ["standard", "prettier"],
parserOptions: {
ecmaVersion: 12,
},
rules: {},
};
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
.env
.idea
.vscode
.vscode
.DS_Store
23 changes: 23 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# What enviorment are we creating
FROM node:18

# Where is all the code going to lie in this enviroment
WORKDIR /app

# What is needed for this enviroment to run properly and where are we storing it in our folder
COPY package*.json ./

# What command is needed to run for this app to work
RUN npm install

# What code am I copying, and where is it going (first . is copy everything, second . is put it in the root of the directory)
COPY . .

# Send over any .env variables to the enviroment
ENV PORT=3000

# What Port is our enviroment listening for
EXPOSE 3000

# How do we start the app
CMD ["npm", "run", "start:dev" ]
40 changes: 23 additions & 17 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
const express = require('express')
const logger = require('morgan')
const cors = require('cors')
import express from "express";
import logger from "morgan";
import cors from "cors";
import { router as contactsRouter } from "./routes/api/contactsRouter.js";
import { router as usersRouter } from "./routes/api/usersRouter.js";

const contactsRouter = require('./routes/api/contacts')
const app = express();

const app = express()
const formatsLogger = app.get("env") === "development" ? "dev" : "short";

const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short'
app.use(logger(formatsLogger));
app.use(cors());
app.use(express.json());

app.use(logger(formatsLogger))
app.use(cors())
app.use(express.json())
// tells Express to serve static files from the public directory)
// open http://localhost:3000/avatars/665c98dca10f7f28dc9eb8b2.jpeg on browser
app.use(express.static("public"));

app.use('/api/contacts', contactsRouter)
app.use("/api/contacts", contactsRouter);
app.use("/api/users", usersRouter);

app.use((req, res) => {
res.status(404).json({ message: 'Not found' })
})
app.use((_req, res) => {
res.status(404).json({ message: "Not found" });
});

app.use((err, req, res, next) => {
res.status(500).json({ message: err.message })
})
app.use((err, _req, res, _next) => {
const { status = 500, message = "Server error" } = err;
res.status(status).json({ message });
});

module.exports = app
export { app };
93 changes: 93 additions & 0 deletions controllers/contactsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { Contact } from "../models/contactsModel.js";
// prettier-ignore
import { contactValidation, favoriteValidation } from "../validations/validation.js";
import { httpError } from "../helpers/httpError.js";

const getAllContacts = async (req, res) => {
const { page = 1, limit = 20, favorite } = req.query;
const query = favorite ? { favorite: true } : {};

const result = await Contact.find(query)
.skip((page - 1) * limit)
.limit(parseInt(limit));

res.json(result);
};

const getContactById = async (req, res) => {
const { contactId } = req.params;
const result = await Contact.findById(contactId);

if (!result) {
throw httpError(404, "Contact ID Not Found");
}

res.json(result);
};

const addContact = async (req, res) => {
// Preventing lack of necessary data for contacts (check validations folder)
const { error } = contactValidation.validate(req.body);

if (error) {
throw httpError(400, "missing required fields");
}

const result = await Contact.create(req.body);

res.status(201).json(result);
};

const deleteContactById = async (req, res) => {
const { contactId } = req.params;
const result = await Contact.findByIdAndDelete(contactId);

if (!result) {
throw httpError(404);
}

res.json({
message: "Contact deleted",
});
};

const updateContactById = async (req, res) => {
// Preventing lack of necessary data for contacts (check validations folder)
const { error } = contactValidation.validate(req.body);
if (error) {
throw httpError(400, "missing fields");
}

const { contactId } = req.params;
const result = await Contact.findByIdAndUpdate(contactId, req.body, {
new: true,
});

if (!result) {
throw httpError(404);
}

res.json(result);
};

const updateStatusContact = async (req, res) => {
// Preventing lack of necessary data for favorite (check validations folder)
const { error } = favoriteValidation.validate(req.body);
if (error) {
throw httpError(400, "missing field favorite");
}

const { contactId } = req.params;
const result = await Contact.findByIdAndUpdate(contactId, req.body, {
new: true,
});

if (!result) {
throw httpError(404);
}

res.json(result);
};

// prettier-ignore
export { getAllContacts, getContactById, addContact, deleteContactById, updateContactById, updateStatusContact};
Loading