express-api-cli
Version:
Cli tool for generating an express project. Instead of wasting extra time creating your project structure, start building right away
46 lines (38 loc) • 960 B
text/typescript
import User from '../models/user.model';
import { IUser } from '../interfaces/user.interface';
class UserService {
//get all users
public getAllUsers = async (): Promise<IUser[]> => {
const data = await User.find();
return data;
};
//create new user
public newUser = async (body: IUser): Promise<IUser> => {
const data = await User.create(body);
return data;
};
//update a user
public updateUser = async (_id: string, body: IUser): Promise<IUser> => {
const data = await User.findByIdAndUpdate(
{
_id
},
body,
{
new: true
}
);
return data;
};
//delete a user
public deleteUser = async (_id: string): Promise<string> => {
await User.findByIdAndDelete(_id);
return '';
};
//get a single user
public getUser = async (_id: string): Promise<IUser> => {
const data = await User.findById(_id);
return data;
};
}
export default UserService;