Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PORT=4000
DATABASE_URL="<your_database_url>?schema=prisma"
SHADOW_DATABASE_URL="<your_shadow_database_url>?schema=shadow"
DATABASE_URL=
SHADOW_DATABASE_URL=
JWT_SECRET="somesecurestring"
JWT_EXPIRY="24h"
48 changes: 48 additions & 0 deletions src/controllers/profile.js
Original file line number Diff line number Diff line change
@@ -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 } })
}
115 changes: 115 additions & 0 deletions src/domain/profile.js
Original file line number Diff line number Diff line change
@@ -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))
}
}
Loading