From 292072fec9f8e8e5809eecea5567b00887c75a2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matilda=20S=C3=B6nnergaard?= Date: Thu, 27 Feb 2025 09:42:56 +0100 Subject: [PATCH 1/2] Create Profile model --- src/domain/profile.js | 115 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/domain/profile.js diff --git a/src/domain/profile.js b/src/domain/profile.js new file mode 100644 index 00000000..906ba2fb --- /dev/null +++ b/src/domain/profile.js @@ -0,0 +1,115 @@ +import dbClient from '../utils/dbClient' + +export default class Profile { + static fromDb(profile) { + return new Profile( + profile.id, + profile.userId, + profile.firstName, + profile.lastName, + profile.bio, + profile.githubUrl + ) + } + + static async fromJson(json) { + const { userId, firstName, lastName, bio, githubUrl } = json + + return new Profile(null, userId, firstName, lastName, bio, githubUrl) + } + + constructor(id, userId, firstName, lastName, bio, githubUrl) { + this.id = id + this.userId = userId + this.firstName = firstName + this.lastName = lastName + this.bio = bio + this.githubUrl = githubUrl + } + + toJson() { + return { + profile: { + id: this.id, + user_id: this.userId, + firstName: this.firstName, + lastName: this.lastName, + bio: this.bio, + githubUrl: this.githubUrl + } + } + } + + async save() { + const data = { + firstName: this.firstName, + lastName: this.lastName, + bio: this.bio, + githubUrl: this.githubUrl + } + + if (this.userId) { + data.user = { + connectOrCreate: { + id: this.userId + } + } + } + + const createdProfile = await dbClient.profile.create({ + data, + include: { + user: true + } + }) + return Profile.fromDb(createdProfile) + } + + static async findById(id) { + return Profile._findByUnique('id', id) + } + + static async findAll() { + return Profile._findMany() + } + + static async findByUserId(userId) { + return Profile._findByUnique('userId', userId) + } + + static async _findByUnique(key, value) { + const foundProfile = await dbClient.profile.findUnique({ + where: { + [key]: value + }, + include: { + user: true + } + }) + + if (foundProfile) { + return Profile.fromDb(foundProfile) + } + + return null + } + + static async _findMany(key, value) { + const query = { + include: { + user: true + } + } + + if (key && value) { + query.where = { + user: { + [key]: value + } + } + } + + const foundProfiles = await dbClient.profile.findMany(query) + return foundProfiles.map((p) => Profile.fromDb(p)) + } +} From fd5836ab3fc0b72dd49c8e9e0f36eb53cafcebc5 Mon Sep 17 00:00:00 2001 From: Wilmer Winkler Date: Thu, 27 Feb 2025 10:21:16 +0100 Subject: [PATCH 2/2] Profile controller made --- .env.example | 4 ++-- src/controllers/profile.js | 48 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 src/controllers/profile.js diff --git a/.env.example b/.env.example index 932b9f1e..d3c91282 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,5 @@ PORT=4000 -DATABASE_URL="?schema=prisma" -SHADOW_DATABASE_URL="?schema=shadow" +DATABASE_URL= +SHADOW_DATABASE_URL= JWT_SECRET="somesecurestring" JWT_EXPIRY="24h" \ No newline at end of file diff --git a/src/controllers/profile.js b/src/controllers/profile.js new file mode 100644 index 00000000..cca9b0d8 --- /dev/null +++ b/src/controllers/profile.js @@ -0,0 +1,48 @@ +import Profile from '../domain/profile.js' +import { sendDataResponse, sendMessageResponse } from '../utils/responses.js' + +export const create = async (req, res) => { + const profileToCreate = await Profile.fromJson(req.body) + + try { + const existingProfileWithUser = await Profile.findByUserId( + profileToCreate.userId + ) + + if (existingProfileWithUser) { + return sendDataResponse(res, 400, { email: 'You already have a profile' }) + } + + const createdProfile = await profileToCreate.save() + + return sendDataResponse(res, 201, createdProfile) + } catch (error) { + return sendMessageResponse(res, 500, 'Unable to create new profile') + } +} + +export const getById = async (req, res) => { + const id = parseInt(req.params.id) + + try { + const foundProfile = await Profile.findById(id) + + if (!foundProfile) { + return sendDataResponse(res, 404, { id: 'User not found' }) + } + + return sendDataResponse(res, 200, foundProfile) + } catch (e) { + return sendMessageResponse(res, 500, 'Unable to get user') + } +} + +export const updateById = async (req, res) => { + const { user_id: userId } = req.body + + if (!userId) { + return sendDataResponse(res, 400, { user_id: 'User ID is required' }) + } + + return sendDataResponse(res, 201, { user: { user_id: userId } }) +}