From 4967b187c7cd0a72709a422f81ae95fcfdfb958d Mon Sep 17 00:00:00 2001
From: Nabilsenko <96882497+Nabilsenko@users.noreply.github.com>
Date: Wed, 9 Nov 2022 23:46:43 +0100
Subject: [PATCH] feat: added get-people, with people files

---
 src/people/dao/people.dao.ts         | 19 +++++++++
 src/people/dto/create-people.dto.ts  |  0
 src/people/entities/people.entity.ts |  8 ++++
 src/people/people.controller.ts      | 29 +++++++++++++
 src/people/people.service.ts         | 34 +++++++++++++++
 src/people/people.types.ts           |  9 ++++
 src/people/schemas/people.schema.ts  | 64 ++++++++++++++++++++++++++++
 7 files changed, 163 insertions(+)
 create mode 100644 src/people/dao/people.dao.ts
 create mode 100644 src/people/dto/create-people.dto.ts
 create mode 100644 src/people/entities/people.entity.ts
 create mode 100644 src/people/people.controller.ts
 create mode 100644 src/people/people.service.ts
 create mode 100644 src/people/people.types.ts
 create mode 100644 src/people/schemas/people.schema.ts

diff --git a/src/people/dao/people.dao.ts b/src/people/dao/people.dao.ts
new file mode 100644
index 0000000..273eb67
--- /dev/null
+++ b/src/people/dao/people.dao.ts
@@ -0,0 +1,19 @@
+import { Injectable } from '@nestjs/common';
+import { InjectModel } from '@nestjs/mongoose';
+import { Model } from 'mongoose';
+import { from, map, Observable } from 'rxjs';
+/*import { CreatePeopleDto } from '../dto/create-people.dto';
+import { UpdatePeopleDto } from '../dto/update-people.dto';*/
+import { People } from '../schemas/people.schema';
+
+@Injectable()
+export class PeopleDao {
+  constructor(
+    @InjectModel(People.name)
+    private readonly _groupModel: Model<People>,
+  ) {}
+
+  find = (): Observable<People[]> =>
+    from(this._groupModel.find({})).pipe(map((people) => [].concat(people)));
+
+}
\ No newline at end of file
diff --git a/src/people/dto/create-people.dto.ts b/src/people/dto/create-people.dto.ts
new file mode 100644
index 0000000..e69de29
diff --git a/src/people/entities/people.entity.ts b/src/people/entities/people.entity.ts
new file mode 100644
index 0000000..4b2a04f
--- /dev/null
+++ b/src/people/entities/people.entity.ts
@@ -0,0 +1,8 @@
+import { People } from '../schemas/people.schema';
+
+export class PeopleEntity {
+    
+  constructor(partial: Partial<People>) {
+    Object.assign(this, partial);
+  }
+}
\ No newline at end of file
diff --git a/src/people/people.controller.ts b/src/people/people.controller.ts
new file mode 100644
index 0000000..a24ccc5
--- /dev/null
+++ b/src/people/people.controller.ts
@@ -0,0 +1,29 @@
+import {
+    Controller,
+    Get,
+    Post,
+    Put,
+    Delete,
+    Param,
+    Body,
+    UseInterceptors,
+  } from '@nestjs/common';
+  import { Observable } from 'rxjs';
+//    import { HttpInterceptor } from '../interceptors/http.interceptor';
+//   import { CreatePeopleDto } from './dto/create-people.dto';
+//   import { UpdatePeopleDto } from './dto/update-people.dto';
+  import { PeopleEntity } from './entities/people.entity';
+  import { PeopleService } from './people.service';
+  
+
+  @Controller('people')
+//   @UseInterceptors(HttpInterceptor)
+  export class PeopleController {
+    constructor(private readonly _peopleService: PeopleService) {}
+  
+    @Get()
+    findAll(): Observable<PeopleEntity[] | void> {
+      return this._peopleService.findAll();
+    }
+  
+  }
\ No newline at end of file
diff --git a/src/people/people.service.ts b/src/people/people.service.ts
new file mode 100644
index 0000000..41a31db
--- /dev/null
+++ b/src/people/people.service.ts
@@ -0,0 +1,34 @@
+import {
+    Injectable,
+    UnprocessableEntityException,
+    NotFoundException,
+    ConflictException,
+  } from '@nestjs/common';
+  import {
+    Observable,
+    of,
+    filter,
+    map,
+    mergeMap,
+    defaultIfEmpty,
+    catchError,
+    throwError,
+  } from 'rxjs';
+import { PeopleDao } from './dao/people.dao';
+//    import { HttpInterceptor } from '../interceptors/http.interceptor';
+//   import { CreatePeopleDto } from './dto/create-people.dto';
+//   import { UpdatePeopleDto } from './dto/update-people.dto';
+import { PeopleEntity } from './entities/people.entity';
+
+  
+  @Injectable()
+  export class PeopleService {
+    constructor(private readonly _peopleDao: PeopleDao) {}
+  
+    findAll = (): Observable<PeopleEntity[] | void> =>
+      this._peopleDao.find().pipe(
+        filter(Boolean),
+        map((people) => (people || []).map((person) => new PeopleEntity(person))),
+        defaultIfEmpty(undefined),
+      );
+}
\ No newline at end of file
diff --git a/src/people/people.types.ts b/src/people/people.types.ts
new file mode 100644
index 0000000..2e768bb
--- /dev/null
+++ b/src/people/people.types.ts
@@ -0,0 +1,9 @@
+export type Group = {
+    _id: any;
+    firstname: string,
+    lastname: string,
+    email: string,
+    passwordHash: string,
+    role: number,
+    groups: string[]
+  };
diff --git a/src/people/schemas/people.schema.ts b/src/people/schemas/people.schema.ts
new file mode 100644
index 0000000..fe0f91c
--- /dev/null
+++ b/src/people/schemas/people.schema.ts
@@ -0,0 +1,64 @@
+import {
+    Prop, raw, Schema, SchemaFactory,
+} from '@nestjs/mongoose';
+import * as mongoose from 'mongoose';
+
+@Schema({
+    toJSON: {
+        virtuals: true,
+        transform: (dpc: any, ret: any) => { 
+            delete ret._id;
+        },
+    },
+    versionKey: false,
+
+})
+export class People {
+    @Prop({
+        type: mongoose.Schema.Types.ObjectId,
+        auto: true,
+    }) _id: any;
+
+    @Prop({
+        type: String,
+        required: true,
+        trim: true,
+        minlength: 2,
+    }) firstname: string;
+
+    @Prop({
+        type: String,
+        required: true,
+        trim: true,
+        minlength: 2,
+    }) lastname: string;
+
+    @Prop({
+        type: String,
+        required: true,
+        trim: true,
+        minlength: 2,
+    }) email: string;
+
+    @Prop({
+        type: String,
+        required: true,
+        trim: true,
+        minlength: 2,
+    }) passwordHash: string;
+
+    @Prop({
+        type: Number,
+        required: true,
+    }) role: string;
+
+    @Prop({
+        type: [String],
+        required: true,
+    }) groups: string[];
+
+}
+
+export const PeopleSchema = SchemaFactory.createForClass(People);
+
+PeopleSchema.index({ name: 1 }, { unique: true });
\ No newline at end of file
-- 
GitLab