SuperXtra / NotepadApi

0 stars 0 forks source link

Don't use DTOs #16

Open patrykcelinski opened 4 years ago

patrykcelinski commented 4 years ago

https://github.com/SuperXtra/NotepadApi/blob/be211e4f50aaee61a7bcfeb548bd2376fccf6aa6/app/model/dto/NoteDto.scala#L5

Move these classes to companion object of their controllers and rename e.g. NoteDto -> CreateNoteRequest, RegisterUserRequest. Make them private for controller so it won't leak to service layer.

This is bad:

  def createNote(note: NoteDto): Future[Int] = userRepository.createNote(Note(0, note.title, note.body, note.userId))

Should be something like::

def create(title: String, body: String, userId: UserId): Future[Int] = userRepository.createNote(Note(randomUUID(), note.title, note.body, note.userId))

You should use these CreateNoteRequest object just to deserialize request entity. Then service shouldn't know about them.

BTW there is a bug in createNote method that Id was always 0.

SuperXtra commented 4 years ago
object HomeController {

  case class CreateNoteRequest(title: String, body: String, userId: Int)
  case class RegisterUserRequest(name: String, lastName: String, email: String, password: String)
  case class UpdatePasswordRequest(userId: Int, oldPassword: String, newPassword: String)

  implicit val readsCreateNoteRequest: Reads[CreateNoteRequest] = Json.reads[CreateNoteRequest]
  implicit val readsUserNoteRequest: Reads[RegisterUserRequest] = Json.reads[RegisterUserRequest]
  implicit val readsUpdatePasswordRequest: Reads[UpdatePasswordRequest] = Json.reads[UpdatePasswordRequest]
}

zastanawiam sie jak ograniczyc dostep tylko dla controllera, private[HomeController] nie dziala