DELETE

Adil

DevBest CEO
May 28, 2011
1,278
716
Any 'service' classes would contain business logic... so a 'UserService' class would contain code for interacting and performing operations with user objects.
E.g.

Code:
class UserService {

signIn(email, password) {
...
}

makeAdmin(user) {
...
}

...

I would then inject these service classes using a dependency injection framework
 

Adil

DevBest CEO
May 28, 2011
1,278
716

PHP:
import JWT from '~/app/lib/jwt'
import Database from '~/app/sql/interactors/session'
export default class SessionService {

  static create(user) {
    return new Promise(async (resolve, reject) => {
      let session = {}
      session = await Database.create(user)
      session = await JWT.sign(session)
      resolve(session)
    })
  }

  static read(session) {
    return new Promise(async (resolve, reject) => {
      // Validate JWT
      session = await JWT.validate(session)
      // Check Session State (Must be "allowed") and return user
      session = await Database.read(session.id, true)
      if (session) {
        resolve({session: session.id, user: session.user})
      } else {
        reject()
      }
    })
  }

  static delete(session) {
    return Database.delete(session)
  }

}


PHP:
import Database from '~/app/sql/interactors/user'
export default class UserService {

  static login(username, password) {
    return new Promise(async (resolve, reject) => {
      let user = {}
      // Fetch User
      user = await Database.read(username, 'private')
      await user.verifyPassword(password)
      resolve(user)
    })
  }

  static register(username, password, mail, ip) {
    return new Promise(async (resolve, reject) => {
      try {
        // Create User
        user = await Database.create({username: username, password: password, mail: mail, ip: ip})
        resolve(user)
      } catch (error) {
        reject(error)
      }
    })
  }

  static update(user) {
    return new Promise((resolve, reject) => {})
  }
}

So this, essentially?
Yep, although if I were to 'modularise' my application, I _probably_ wouldn't use static methods in my services (I know in Java, static methods can be unwieldy when testing your application.. not sure about ES6; I reckon it must be a similar situation there?)
 

RastaLulz

fight teh power
Staff member
May 3, 2010
3,934
3,933
What does a "service" mean to you? If your service is making direct queries to the same database as all of you other services, that seems rather tightly coupled, which goes against the idea of service-oriented architecture. The class that you have above looks like a rather generic model.

As for static's, I don't see a problem with using them so long as the method doesn't have any state associated to it (e.g. data in, data out).
 

Quackster

a devbest user says what
Aug 22, 2010
1,765
1,245
Just a do monolithic design, it's worked good for years and it will work well for years to come.

PHP, NGNIX, Node.js, Linux kernel, are all monolithic designs. It depends on what program you're planning to create, honestly. The things which give a monolithic design a bad rep is when you have I/O access all over the damn source instead of reusing functions explicitly rewritten for that purpose.
 

MayoMayn

BestDev
Oct 18, 2016
1,423
683

PHP:
import JWT from '~/app/lib/jwt'
import Database from '~/app/sql/interactors/session'
export default class SessionService {

  static create(user) {
    return new Promise(async (resolve, reject) => {
      let session = {}
      session = await Database.create(user)
      session = await JWT.sign(session)
      resolve(session)
    })
  }

  static read(session) {
    return new Promise(async (resolve, reject) => {
      // Validate JWT
      session = await JWT.validate(session)
      // Check Session State (Must be "allowed") and return user
      session = await Database.read(session.id, true)
      if (session) {
        resolve({session: session.id, user: session.user})
      } else {
        reject()
      }
    })
  }

  static delete(session) {
    return Database.delete(session)
  }

}


PHP:
import Database from '~/app/sql/interactors/user'
export default class UserService {

  static login(username, password) {
    return new Promise(async (resolve, reject) => {
      let user = {}
      // Fetch User
      user = await Database.read(username, 'private')
      await user.verifyPassword(password)
      resolve(user)
    })
  }

  static register(username, password, mail, ip) {
    return new Promise(async (resolve, reject) => {
      try {
        // Create User
        user = await Database.create({username: username, password: password, mail: mail, ip: ip})
        resolve(user)
      } catch (error) {
        reject(error)
      }
    })
  }

  static update(user) {
    return new Promise((resolve, reject) => {})
  }
}

So this, essentially?
I don't get the way you're using async / await.

Simply just return the value instead of creating a new Promise using the constructor.

PHP:
static async create(user) {
     const session = await Database.create(user)
     const token = await JWT.sign(session)

     return token
}

And if you need to reject something inside async / await function simply throw an error.

PHP:
static async read(session) {
     const jwt = await JWT.validate(session)
     const session = await Database.read(session.id, true)

     if (!session) throw new Error('invalid session')

     return { session: session.id, user: session.user }  
}
 
Last edited:

Users who are viewing this thread

Top