Skip to content

Commit

Permalink
23. Added Gender and Weight (#26)
Browse files Browse the repository at this point in the history
* 9. Implemented User Controller Service

* 18. Extended the UserEntity with favouriteDrinks

* Delete line 4 in users.controller.ts

* Changed the Requested Corrections

* Requested Corrections

* 23. Add Gender and Weight to User

* Requested Corrections (Not Working)

* Requested Corrections (But Now It Actualy Works)
  • Loading branch information
VarMattew authored Jul 13, 2024
1 parent 887fc86 commit f622a9a
Show file tree
Hide file tree
Showing 10 changed files with 1,173 additions and 962 deletions.
1 change: 1 addition & 0 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"test:cov": "jest --coverage"
},
"dependencies": {
"@kir-dev/passport-authsch": "^0.2.1",
"@nestjs/cli": "^10.3.2",
"@nestjs/common": "^10.3.8",
"@nestjs/config": "^3.2.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- CreateEnum
CREATE TYPE "Gender" AS ENUM ('Male', 'Female');

-- AlterTable
ALTER TABLE "User" ADD COLUMN "gender" "Gender",
ADD COLUMN "weight" DOUBLE PRECISION;
10 changes: 9 additions & 1 deletion apps/backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

generator client {
provider = "prisma-client-js"
provider = "prisma-client-js"
previewFeatures = ["omitApi"]
}

datasource db {
Expand All @@ -20,11 +21,18 @@ enum DrinkType {
COCKTAIL
}

enum Gender {
Male
Female
}

model User {
authSchId String @id
email String @unique
firstName String
lastName String
gender Gender?
weight Float?
profilePictureUrl String?
// ownedEvents Event[]
// drinkActions DrinkAction[]
Expand Down
22 changes: 22 additions & 0 deletions apps/backend/src/users/dto/user-gender-weight.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Gender } from '@prisma/client';
import { IsEnum, IsOptional, Min } from 'class-validator';
import { IsFloat } from 'src/util/is-float.validator';

export class UserGenderWeight {
/**
* Gender
* @example Male
*/
@IsEnum(Gender)
@IsOptional()
gender?: Gender;

/**
* Weight
* @example 75.2
*/
@IsFloat()
@Min(0)
@IsOptional()
weight?: number;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Drink } from 'src/drinks/entities/drink.entity';

import { User } from './user.entity';
import { User } from '../entities/user.entity';

export class UserWithFavoriteDrinks extends User {
/**
Expand Down
5 changes: 5 additions & 0 deletions apps/backend/src/users/dto/user-without-weight.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { OmitType } from '@nestjs/swagger';

import { User } from '../entities/user.entity';

export class UserWithoutWeight extends OmitType(User, ['weight']) {}
19 changes: 17 additions & 2 deletions apps/backend/src/users/entities/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IsNotEmpty, IsOptional, IsString, IsUUID } from 'class-validator';

import { Gender } from '@prisma/client';
import { IsNotEmpty, IsOptional, IsString, IsUUID, Min } from 'class-validator';
export class User {
@IsUUID()
authSchId: string;
Expand Down Expand Up @@ -28,6 +28,21 @@ export class User {
@IsNotEmpty()
lastName: string;

/**
* Gender
* @example Male/Female
*/
@IsOptional()
gender?: Gender;

/**
* Weight
* @example 75.2 (kg)
*/
@IsOptional()
@Min(0)
weight?: number;

@IsOptional()
profilePictureUrl?: string;
}
18 changes: 14 additions & 4 deletions apps/backend/src/users/users.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Controller, Delete, Get, Param, ParseUUIDPipe, Post } from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { Body, Controller, Delete, Get, Param, ParseUUIDPipe, Patch, Post } from '@nestjs/common';
import { ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger';
import { User } from '@prisma/client';

import { UserWithFavoriteDrinks } from './entities/UserWithFavoriteDrinks';
import { UserGenderWeight } from './dto/user-gender-weight.dto';
import { UserWithFavoriteDrinks } from './dto/user-with-favorite-drinks.dto';
import { UserWithoutWeight } from './dto/user-without-weight.dto';
import { UsersService } from './users.service';
@ApiTags('Users')
@Controller('users')
Expand All @@ -17,7 +19,7 @@ export class UsersController {

@Get()
@ApiOperation({ summary: 'Get all users' })
async findAll(): Promise<User[]> {
async findAll(): Promise<UserWithoutWeight[]> {
return await this.usersService.findAll();
}

Expand All @@ -27,6 +29,14 @@ export class UsersController {
return await this.usersService.remove(id);
}

@Patch(':id/')
@ApiOperation({ summary: 'Edit Gender and Weight' })
@ApiBody({ type: UserGenderWeight })
async update(@Param('id', ParseUUIDPipe) id: string, @Body() userGenderWeight: UserGenderWeight) {
await this.usersService.update(id, userGenderWeight);
return { message: `User was updated successfully: ${userGenderWeight.gender}, ${userGenderWeight.weight} kg` };
}

@Post(':id/favoriteDrinks/:favoriteDrinkId')
addFavoriteDrink(
@Param('id', new ParseUUIDPipe()) id: string,
Expand Down
25 changes: 21 additions & 4 deletions apps/backend/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,39 @@ import { BadRequestException, Injectable, NotFoundException } from '@nestjs/comm
import { User } from '@prisma/client';
import { PrismaService } from 'nestjs-prisma';

import { UserWithFavoriteDrinks } from './entities/UserWithFavoriteDrinks';
import { UserGenderWeight } from './dto/user-gender-weight.dto';
import { UserWithFavoriteDrinks } from './dto/user-with-favorite-drinks.dto';
import { UserWithoutWeight } from './dto/user-without-weight.dto';

@Injectable()
export class UsersService {
constructor(readonly prisma: PrismaService) {}

async update(authSchId: string, data: UserGenderWeight) {
try {
return await this.prisma.user.update({
where: { authSchId },
data: { gender: data.gender, weight: data.weight },
});
} catch {
throw new NotFoundException('User not found');
}
}

async findOne(authSchId: string): Promise<UserWithFavoriteDrinks> {
const user = await this.prisma.user.findUnique({ where: { authSchId }, include: { favouriteDrinks: true } });
const user = await this.prisma.user.findUnique({
where: { authSchId },
include: { favouriteDrinks: true },
omit: { weight: true },
});
if (!user) {
throw new NotFoundException('User not found');
}
return user;
}

async findAll(): Promise<User[]> {
return await this.prisma.user.findMany();
async findAll(): Promise<UserWithoutWeight[]> {
return await this.prisma.user.findMany({ omit: { weight: true } });
}

async remove(authSchId: string): Promise<User> {
Expand Down
Loading

0 comments on commit f622a9a

Please sign in to comment.