import {Boulder} from "../models/BoulderDocumentModel";
class BoulderRouteStore {

    async add(boulderRouteTitle: string, boulderRouteDescription: string, difficulty: number, boulderRouteDone: any, imgName: any): Promise<any> {
        if (boulderRouteDone === 'on') {
            boulderRouteDone = true;
        }
        const boulderRoute = new Boulder({boulderRouteTitle: boulderRouteTitle, boulderRouteDescription: boulderRouteDescription, routeDifficulty: difficulty, boulderRouteDone: boulderRouteDone, imgName: imgName});
        return boulderRoute.save();
    }

    async update(done: boolean, title?: string, description?: string, difficulty?: number, id?: string, imgName?: any): Promise<any | void> {
        if (id) {
            if (title && description && difficulty) {
                return Boulder.updateOne(
                    {_id: id},
                    { $set: {
                            boulderRouteTitle: title,
                            boulderRouteDescription: description,
                            routeDifficulty: difficulty,
                            boulderRouteDone: done,
                            imgName: imgName
                        } }
                )
            }
            else {
                return Boulder.updateOne(
                    {_id: id},
                    {$set: {
                            boulderRouteDone: done
                        } }
                )
            }
        }
    }

    async getOne(id: string): Promise<any | void> {
        return Boulder.find({_id: id}).lean();
    }

    async get(sortField: string, sortOrder: number, query?: any): Promise<any> {
        const result = await Boulder.find( query ).lean().sort(sortField);
        return sortOrder === -1 ? result.reverse() : result;
    }
}

export const boulderRouteStore = new BoulderRouteStore();
