Commit d5c731ca authored by Цой Данил's avatar Цой Данил 💬

Rewrote repository and added user model

parent 9db8c85c
APP_PORT=8000
PG_HOST=localhost
MONGO_CLIENT_URL=mongodb://localhost/TsoyDanilMusicDB
\ No newline at end of file
MONGO_CLIENT_URL=mongodb://localhost/TsoyDanilMusicDB
ENV_SALT=10
\ No newline at end of file
This diff is collapsed.
......@@ -11,10 +11,13 @@
"license": "ISC",
"dependencies": {
"@types/cors": "^2.8.13",
"@types/jsonwebtoken": "^9.0.1",
"@types/shortid": "^0.0.29",
"bcrypt": "^5.1.0",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.0",
"mongodb": "^5.1.0",
"mongoose": "^7.0.1",
"multer": "^1.4.5-lts.1",
......@@ -26,6 +29,7 @@
"uuid": "^9.0.0"
},
"devDependencies": {
"@types/bcrypt": "^5.0.0",
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.17",
"@types/mongodb": "^4.0.7",
......
import {Document} from "mongoose"
export default interface IUser extends Document {
_id: string
username: string
password: string
token: string
}
\ No newline at end of file
import IUser from "./IUser";
export default interface IUserDto {
username: IUser['username']
password: IUser['password']
}
import mongoose, { Schema } from "mongoose";
import bcrypt from "bcrypt";
import IUser from "../interfaces/IUser";
import jwt from 'jsonwebtoken'
import { v4 } from "uuid";
const UserSchema: Schema = new Schema<IUser>({
username: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
token: {
type: String,
required: true
}
},{versionKey: false})
UserSchema.pre('save', async function(next){
if (!this.isModified('password')) return next()
const salt = await bcrypt.genSalt()
const hash = await bcrypt.hash(this.password, salt)
this.password = hash
next()
})
UserSchema.set('toJSON', {transform(doc, ret, options) {
delete ret.password
return ret
}})
UserSchema.methods.checkPassword = async function(password: string) {
return await bcrypt.compare(password, this.password);
}
UserSchema.methods.generateToken = async function() {
this.token = v4()
};
export const User = mongoose.model<IUser>('User', UserSchema)
\ No newline at end of file
import dotenv from 'dotenv';
import { Request } from "express";
import mongoose, { Mongoose } from 'mongoose';
import { EStatuses } from '../enum/EStatuses';
import IAlbum from '../interfaces/IAlbum';
import IAlbumDto from '../interfaces/IAlbumDto';
import IArtist from '../interfaces/IArtist';
import IArtistDto from '../interfaces/IArtistDto';
import IResponse from '../interfaces/IResponse';
import ITrack from '../interfaces/ITrack';
import ITrackDto from '../interfaces/ITrackDto';
import IUser from '../interfaces/IUser';
import IUserDto from '../interfaces/IUserDto';
import { Album } from '../models/Album';
import { Artist } from '../models/Artist';
import { Track } from '../models/Track';
import { User } from '../models/User';
dotenv.config();
export class MongooseDB {
......@@ -19,6 +34,170 @@ export class MongooseDB {
console.error('Connected error MongooseDB:', error);
}
}
public getAlbums = async(req: Request): Promise<IResponse<IAlbum[] | IAlbum | null>> => {
try {
let data
if (req.query.artist){
data = await Album.find({artist: req.query.artist}).populate('artist')
} else if (req.params.id){
data = await Album.findById(req.params.id).populate('artist')
} else{
data = await Album.find().populate('artist')
}
if (data === null || data === undefined) throw new Error('No album found')
const response: IResponse<IAlbum[] | IAlbum | null> = {
status: EStatuses.SUCCESS,
result: data,
message: 'Albums found'
}
return response
} catch(err: unknown){
const error = err as Error
const response: IResponse<null> = {
status: EStatuses.FAILURE,
result: null,
message: error.message
}
return response
}
}
public addAlbum = async(albumDto: IAlbumDto): Promise<IResponse<IAlbum | null>> => {
try {
if (albumDto.title.trim() === '' || !albumDto.artist || !albumDto.releaseYear || !albumDto.coverImage) throw new Error('All fields should exist')
const album = new Album(albumDto)
const data = await album.save()
const response: IResponse<IAlbum> = {
status: EStatuses.SUCCESS,
result: data,
message: 'Album added'
}
return response
} catch(err: unknown){
const error = err as Error
const response: IResponse<null> = {
status: EStatuses.FAILURE,
result: null,
message: error.message
}
return response
}
}
public getArtists = async(): Promise<IResponse<IArtist[] | null>> => {
try{
const data = await Artist.find()
const response: IResponse<IArtist[]> = {
status: EStatuses.SUCCESS,
result: data,
message: 'Artists found'
}
return response
} catch (err: unknown){
const error = err as Error
const response: IResponse<null> = {
status: EStatuses.FAILURE,
result: null,
message: error.message
}
return response
}
}
public addArtist = async(artistDto: IArtistDto): Promise<IResponse<IArtist | null>> => {
try{
if (artistDto.name.trim() === '' || !artistDto.photo || artistDto.information.trim() === '') throw new Error ('All fields should exist')
const artist = new Artist(artistDto)
const data = await artist.save()
const response: IResponse<IArtist> = {
status: EStatuses.SUCCESS,
result: data,
message: 'Artist added'
}
return response
} catch (err: unknown){
const error = err as Error
const response: IResponse<null> = {
status: EStatuses.FAILURE,
result: null,
message: error.message
}
return response
}
}
public getTracks = async(req: Request): Promise<IResponse<ITrack[] | null>> => {
try{
let data
if (req.query.album){
data = await Track.find({album: req.query.album}).populate('album')
} else if (req.query.artist){
data = await Track.find({album: await Album.find({artist: req.query.artist})}).populate('album')
} else{
data = await Track.find().populate('album')
}
const response: IResponse<ITrack[] | null> = {
status: EStatuses.SUCCESS,
result: data,
message: 'Tracks found'
}
return response
} catch(err: unknown){
const error = err as Error
const response: IResponse<null> = {
status: EStatuses.FAILURE,
result: null,
message: error.message
}
return response
}
}
public addTrack = async(trackDto: ITrackDto): Promise<IResponse<ITrack | null>> => {
try {
if (trackDto.title.trim() === '' || !trackDto.album || !trackDto.length || trackDto.length <= 0) throw new Error('All fields should exist and have valid value')
const album = new Track(trackDto)
const data = await album.save()
const response: IResponse<ITrack> = {
status: EStatuses.SUCCESS,
result: data,
message: 'Album added'
}
return response
} catch(err: unknown){
const error = err as Error
const response: IResponse<null> = {
status: EStatuses.FAILURE,
result: null,
message: error.message
}
return response
}
}
public addUser = async (userDto: IUserDto): Promise<IResponse<IUser | null>> => {
try {
const user = new User(userDto)
const data = await user.save()
const response: IResponse<IUser> = {
status: EStatuses.SUCCESS,
result: data,
message: 'User added'
}
return response
} catch(err: unknown){
const error = err as Error
const response: IResponse<null> = {
status: EStatuses.FAILURE,
result: null,
message: error.message
}
return response
}
}
}
export const mongooseDB = new MongooseDB()
\ No newline at end of file
import { EStatuses } from "../enum/EStatuses";
import IAlbum from "../interfaces/IAlbum";
import IResponse from "../interfaces/IResponse";
import IAlbumDto from "../interfaces/IAlbumDto";
import { Album } from "../models/Album";
import { Request } from "express";
import { mongooseDB } from "../repository/mongooseDB";
export class AlbumsService {
public getAlbums = async(req: Request): Promise<IResponse<IAlbum[] | IAlbum | null>> => {
try {
let data
if (req.query.artist){
data = await Album.find({artist: req.query.artist}).populate('artist')
} else if (req.params.id){
data = await Album.findById(req.params.id).populate('artist')
} else{
data = await Album.find().populate('artist')
}
if (data === null || data === undefined) throw new Error('No album found')
const response: IResponse<IAlbum[] | IAlbum | null> = {
status: EStatuses.SUCCESS,
result: data,
message: 'Albums found'
}
return response
} catch(err: unknown){
const error = err as Error
const response: IResponse<null> = {
status: EStatuses.FAILURE,
result: null,
message: error.message
}
return response
}
return await mongooseDB.getAlbums(req)
}
public addAlbum = async(albumDto: IAlbumDto): Promise<IResponse<IAlbum | null>> => {
try {
if (albumDto.title.trim() === '' || !albumDto.artist || !albumDto.releaseYear || !albumDto.coverImage) throw new Error('All fields should exist')
const album = new Album(albumDto)
const data = await album.save()
const response: IResponse<IAlbum> = {
status: EStatuses.SUCCESS,
result: data,
message: 'Album added'
}
return response
} catch(err: unknown){
const error = err as Error
const response: IResponse<null> = {
status: EStatuses.FAILURE,
result: null,
message: error.message
}
return response
}
return await mongooseDB.addAlbum(albumDto)
}
}
......
import { EStatuses } from "../enum/EStatuses";
import IArtist from "../interfaces/IArtist";
import IArtistDto from "../interfaces/IArtistDto";
import IResponse from "../interfaces/IResponse";
import { Artist } from "../models/Artist";
import { mongooseDB } from "../repository/mongooseDB";
export class ArtistsService {
public getArtists = async(): Promise<IResponse<IArtist[] | null>> => {
try{
const data = await Artist.find()
const response: IResponse<IArtist[]> = {
status: EStatuses.SUCCESS,
result: data,
message: 'Artists found'
}
return response
} catch (err: unknown){
const error = err as Error
const response: IResponse<null> = {
status: EStatuses.FAILURE,
result: null,
message: error.message
}
return response
}
return await mongooseDB.getArtists()
}
public addArtist = async(artistDto: IArtistDto): Promise<IResponse<IArtist | null>> => {
try{
if (artistDto.name.trim() === '' || !artistDto.photo || artistDto.information.trim() === '') throw new Error ('All fields should exist')
const artist = new Artist(artistDto)
const data = await artist.save()
const response: IResponse<IArtist> = {
status: EStatuses.SUCCESS,
result: data,
message: 'Artist added'
}
return response
} catch (err: unknown){
const error = err as Error
const response: IResponse<null> = {
status: EStatuses.FAILURE,
result: null,
message: error.message
}
return response
}
return await mongooseDB.addArtist(artistDto)
}
}
......
import { Request } from "express";
import { EStatuses } from "../enum/EStatuses";
import IResponse from "../interfaces/IResponse";
import ITrack from "../interfaces/ITrack";
import ITrackDto from "../interfaces/ITrackDto";
import { Album } from "../models/Album";
import { Track } from "../models/Track";
import { mongooseDB } from "../repository/mongooseDB";
export class TracksService {
public getTracks = async(req: Request): Promise<IResponse<ITrack[] | null>> => {
try{
let data
if (req.query.album){
data = await Track.find({album: req.query.album}).populate('album')
} else if (req.query.artist){
data = await Track.find({album: await Album.find({artist: req.query.artist})}).populate('album')
} else{
data = await Track.find().populate('album')
}
const response: IResponse<ITrack[] | null> = {
status: EStatuses.SUCCESS,
result: data,
message: 'Tracks found'
}
return response
} catch(err: unknown){
const error = err as Error
const response: IResponse<null> = {
status: EStatuses.FAILURE,
result: null,
message: error.message
}
return response
}
return await mongooseDB.getTracks(req)
}
public addTrack = async(trackDto: ITrackDto): Promise<IResponse<ITrack | null>> => {
try {
if (trackDto.title.trim() === '' || !trackDto.album || !trackDto.length || trackDto.length <= 0) throw new Error('All fields should exist and have valid value')
const album = new Track(trackDto)
const data = await album.save()
const response: IResponse<ITrack> = {
status: EStatuses.SUCCESS,
result: data,
message: 'Album added'
}
return response
} catch(err: unknown){
const error = err as Error
const response: IResponse<null> = {
status: EStatuses.FAILURE,
result: null,
message: error.message
}
return response
}
return await mongooseDB.addTrack(trackDto)
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment