: adds user put to update user
This commit is contained in:
@@ -2,7 +2,7 @@ import "dotenv/config";
|
||||
|
||||
import * as mongoDB from "mongodb";
|
||||
import User from "../models/user";
|
||||
import { InsertOneResult, ObjectId } from "mongodb";
|
||||
import { ObjectId, ReturnDocument } from "mongodb";
|
||||
|
||||
let mongoDb: mongoDB.Db;
|
||||
|
||||
@@ -33,11 +33,13 @@ export class UserService {
|
||||
return this._instance;
|
||||
}
|
||||
|
||||
public async createUser(
|
||||
name: string,
|
||||
uuid: string,
|
||||
): Promise<InsertOneResult> {
|
||||
return await this.collection.insertOne({ name, uuid });
|
||||
public async updateUser(id: string, user: User): Promise<User> {
|
||||
const result = await this.collection.findOneAndUpdate(
|
||||
{ _id: new ObjectId(id) },
|
||||
{ $set: user },
|
||||
{ returnDocument: ReturnDocument.AFTER },
|
||||
);
|
||||
return result as unknown as User;
|
||||
}
|
||||
|
||||
public async getAllUsers(): Promise<User[]> {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { UserService } from "../db/services/database.service";
|
||||
import express from "express";
|
||||
import User from "../db/models/user";
|
||||
|
||||
export class RestUser {
|
||||
constructor(private callback: () => Promise<UserService>) {}
|
||||
@@ -25,18 +26,15 @@ export class RestUser {
|
||||
.send(`Unable to find matching document with id: ${req.params.id}`);
|
||||
});
|
||||
|
||||
router.post("/", async (req, res) => {
|
||||
router.put("/:id", async (req, res) => {
|
||||
const userService = await this.callback();
|
||||
const { name, uuid } = req.body;
|
||||
const result = await userService.createUser(name, uuid);
|
||||
const id = req.params.id;
|
||||
const user = req.body as User;
|
||||
const result = await userService.updateUser(id, user);
|
||||
|
||||
result
|
||||
? res
|
||||
.status(201)
|
||||
.send(
|
||||
`Successfully created a new user with id ${result.insertedId}`,
|
||||
)
|
||||
: res.status(500).send("Failed to create a new game.");
|
||||
? res.status(200).send(result)
|
||||
: res.status(304).send(`User with id: ${id} was not updated.`);
|
||||
});
|
||||
|
||||
return router;
|
||||
|
||||
Reference in New Issue
Block a user