Skip to content
Closed
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
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