Skip to content

Commit 1c53eac

Browse files
Jaime Salas ZancadaJaime Salas Zancada
authored andcommitted
added end points CRUD
1 parent 664d931 commit 1c53eac

File tree

11 files changed

+227
-17
lines changed

11 files changed

+227
-17
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
DATABASE_URL=
2+
DATABASE_NAME=
23
HOST=
34
PORT=

01-contenedores/lemoncode-challenge/node-stack/backend/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The following are environment variables that we can set up:
1212

1313
```ini
1414
DATABASE_URL=
15+
DATABASE_NAME=
1516
HOST=
1617
PORT=
1718
```

01-contenedores/lemoncode-challenge/node-stack/backend/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ if (process.env.NODE_ENV === 'development') {
55
export default {
66
database: {
77
url: process.env.DATABASE_URL || 'mongodb://localhost:27017',
8+
name: process.env.DATABASE_NAME || 'TopicstoreDb'
89
},
910
app: {
1011
host: process.env.HOST || 'localhost',
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// TODO: Move to infrastructure.
2+
// TODO: Create services.
3+
import { Collection, Db, Document, MongoClient } from "mongodb";
4+
import config from '../config';
5+
6+
export type CollectionNames = 'Topics';
7+
8+
const { url } = config.database;
9+
const client = new MongoClient(url);
10+
11+
export const connect = async () => {
12+
try {
13+
await client.connect();
14+
} catch (error) {
15+
console.error();
16+
}
17+
};
18+
19+
export const disconnect = async () => {
20+
await client.close();
21+
};
22+
23+
export const getDatabaseInstance = (databaseName: string): Db =>
24+
client.db(databaseName);
25+
26+
export const getCollection = (db: Db, collection: CollectionNames): Collection<Document> => {
27+
// TODO: Create schemas.
28+
return db.collection(collection);
29+
};
30+
31+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { ObjectId, Collection } from 'mongodb';
2+
import { Topic as TopicEntity } from '../entities';
3+
4+
type Topic = Pick<TopicEntity, 'Name'>;
5+
6+
export interface TopicDatabase extends Topic {
7+
_id?: ObjectId
8+
}
9+
10+
export interface TopicDAL {
11+
createTopic(topic: TopicDatabase): Promise<void>;
12+
getTopics(): Promise<TopicDatabase[]>;
13+
getTopicById(id: string): Promise<TopicDatabase | null>;
14+
updateTopic(id: string, topic: Partial<TopicDatabase>): Promise<void>;
15+
deleteTopic(id: string): Promise<void>;
16+
}
17+
18+
export type TopicDALFactory = (collection: Collection) => TopicDAL;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Collection } from 'mongodb';
2+
import { TopicDAL, TopicDALFactory, TopicDatabase} from './topics.contract.dal';
3+
4+
export const topicDALFactory: TopicDALFactory = (collection: Collection): TopicDAL => {
5+
return {
6+
async createTopic(topic: TopicDatabase): Promise<void> {
7+
try {
8+
await collection.insertOne(topic);
9+
} catch (error) {
10+
console.log(error);
11+
throw error;
12+
}
13+
},
14+
async getTopics(): Promise<TopicDatabase[]> {
15+
try {
16+
const topics = await collection.find({}).toArray();
17+
return topics as TopicDatabase[];
18+
} catch (error) {
19+
console.log(error);
20+
throw error;
21+
}
22+
},
23+
async getTopicById(id: string): Promise<TopicDatabase | null> {
24+
const result = await collection.findOne({
25+
_id: id
26+
});
27+
return result as TopicDatabase;
28+
},
29+
async updateTopic(id: string, topic: Partial<TopicDatabase>): Promise<void> {
30+
delete topic['_id'];
31+
await collection.updateOne({
32+
_id: id
33+
}, {
34+
$set: {
35+
...topic
36+
}
37+
});
38+
},
39+
async deleteTopic(id: string): Promise<void> {
40+
await collection.deleteOne({ _id: id });
41+
}
42+
};
43+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
export interface Topics {
1+
export interface Topic {
2+
id?: string;
23
Name: string;
34
}
Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
11
import express from 'express';
2+
import { Topic } from '../entities';
3+
import { TopicsService } from '../services';
24

3-
export const topicsRoutes = (topicsServiceDAL: any) => {
5+
export const topicsRoutes = (topicsService: TopicsService) => {
46
const router = express.Router();
57

6-
router.get('/', (_, res) => {
7-
// TODO: Use topicsServiceDAL
8-
res.json(['topic A', 'topic B']);
8+
router.get('/', async (_, res) => {
9+
const result = await topicsService.getTopics();
10+
res.json(result);
911
});
1012

11-
router.get('/:id', (_, res) => {
12-
// TODO: Use topicsServiceDAL
13-
res.json('topic A');
13+
router.get('/:id', async (req, res) => {
14+
const { id } = req.params;
15+
const result = await topicsService.getTopicById(id);
16+
res.json(result);
1417
});
1518

16-
router.post('/', (req, res) => {
17-
// TODO: Use topicsServiceDAL
18-
const echo = req.body;
19-
res.json(echo);
19+
router.post('/', async (req, res) => {
20+
const topic = req.body as Topic;;
21+
await topicsService.createTopic(topic);
22+
res.json(topic);
2023
});
2124

22-
router.put('/:id', (req, res) => {
23-
const echo = req.body;
24-
res.json(echo);
25+
router.put('/:id', async (req, res) => {
26+
const { id } = req.params;
27+
const topic = req.body as Topic;
28+
await topicsService.updateTopic(id, topic);
29+
res.json(topic);
2530
});
2631

27-
router.delete('/:id', (req, res) => {
32+
router.delete('/:id', async (req, res) => {
33+
const { id } = req.params;
34+
await topicsService.deleteTopic(id);
2835
res.json();
2936
});
3037

3138
return router;
32-
}
39+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './mappers';
2+
export * from './topics.service';
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { ObjectId } from 'mongodb';
2+
import { Topic as TopicEntity } from '../entities';
3+
import { TopicDatabase} from '../dal/topics.contract.dal';
4+
5+
function isTopicDatabase(topic: TopicEntity | TopicDatabase): topic is TopicDatabase {
6+
if((topic as TopicDatabase)._id) {
7+
return true;
8+
}
9+
return false;
10+
}
11+
12+
function isTopicEntity(topic: TopicEntity | TopicDatabase): topic is TopicEntity {
13+
if((topic as TopicEntity).id) {
14+
return true;
15+
}
16+
return false;
17+
}
18+
19+
export function mapTopic(topic: TopicEntity | TopicDatabase): TopicEntity | TopicDatabase {
20+
if (isTopicDatabase(topic)) {
21+
return {
22+
...topic,
23+
id: topic._id.toString(),
24+
}
25+
}
26+
27+
if(isTopicEntity(topic)) {
28+
return {
29+
...topic,
30+
_id: new ObjectId(topic.id),
31+
}
32+
}
33+
}
34+
35+
export function mapTopics(topics: TopicEntity[] | TopicDatabase[]): TopicEntity[] | TopicDatabase[] {
36+
return topics.map(mapTopic);
37+
}

0 commit comments

Comments
 (0)