Skip to content

Commit

Permalink
Verify club search when posting
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeneos committed Oct 3, 2022
1 parent 6529981 commit a42fd4b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
10 changes: 10 additions & 0 deletions doc/sprint0/setup/backend/routes/clubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ router.route('/add').post((req, res) => {

});

// Find club based on name
router.route('/search').get((req, res) => {

const name = req.body.name;
// Search
Club.find({name: name})
.then(club => res.json(club))
.catch(err => res.status(400).json('Error: ' + err));
});

// Get information about the club on the URI
router.route("/:id").get((req, res) => {
Club.findById(req.params.id)
Expand Down
48 changes: 34 additions & 14 deletions doc/sprint0/setup/backend/routes/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const router = require('express').Router();

// Mongo model
let Post = require('../models/post.model');
let Club = require('../models/club.model');

// Endpoint that takes care of http get requests
router.route('/').get((req, res) => {
Expand All @@ -25,22 +26,41 @@ router.route('/add').post((req, res) => {
const group = req.body.group;
const description = req.body.description;

// Create new post instance
const newPost = new Post({
title,
username,
group,
description,
});

// Save post to database
newPost.save()

// Feedback
.then(() => res.json('Posted'))
// Check if club exists
Club.find({name: group}, (err, clubs) => {

// Error catching
.catch(err => res.status(400).json('Error: ' + err));
if (err) {
res.status(400).json('Error: ' + err);
return;
}

// Checking
if (clubs.length) {

// Create new post instance
const newPost = new Post({
title,
username,
group,
description,
});

// Save post to database
newPost.save()

// Feedback
.then(() => res.json('Posted'))

// Error catching
.catch(err => res.status(400).json('Error: ' + err));

} else {
req.json('Group doesn\'t exist');
}
});


});

// Get request post information based on URI
Expand Down

0 comments on commit a42fd4b

Please sign in to comment.