-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23117e6
commit c810c8c
Showing
3 changed files
with
82 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,10 @@ case class SharedModel( | |
archivo_id: Int | ||
|
||
|
||
)case class SharedCreateModel( | ||
|
||
usuario_id_id: Int, | ||
archivo_id: Int | ||
|
||
|
||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,57 @@ | ||
package routes | ||
|
||
import akka.http.scaladsl.server.Directives._ | ||
import akka.http.scaladsl.server.Route | ||
import controllers.SharedController | ||
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._ | ||
import io.circe.generic.auto._ | ||
import akka.http.scaladsl.model.{HttpResponse, StatusCodes} | ||
import models.{sharedModel, sharedCreateModel} | ||
import scala.concurrent.Future | ||
import com.typesafe.config.ConfigFactory | ||
|
||
class SharedRoute(SharedController: SharedController) { | ||
|
||
val route: Route = pathPrefix("shared") { | ||
get { | ||
path(IntNumber) { id => | ||
val result: Future[Either[String, sharedModel]] = SharedController.buscarUsuario(id) | ||
onSuccess(result) { | ||
case Right(shared) => complete(shared) | ||
case Left(errorMessage) => complete(HttpResponse(StatusCodes.NotFound, entity = errorMessage)) | ||
} | ||
} | ||
} ~ | ||
path("register") { | ||
post { | ||
entity(as[sharedCreateModel]) { shared => | ||
val result: Future[Either[String, sharedModel]] = | ||
SharedController.guardarCompartido(shared.usuario_id, archivo_id) | ||
onSuccess(result) { | ||
case Right(newshared) => complete(StatusCodes.Created, newshared) | ||
case Left(errorMessage) => complete(HttpResponse(StatusCodes.InternalServerError, entity = errorMessage)) | ||
} | ||
} | ||
} | ||
} ~ | ||
path("delete") { | ||
put { | ||
entity(as[List[Int]]) { ids => | ||
val result: Future[List[Either[String, sharedModel]]] = SharedController.eliminarCompartido(ids) | ||
onSuccess(result) { resultList => | ||
val errors = resultList.collect { case Left(errorMessage) => errorMessage } | ||
if (errors.isEmpty) { | ||
val shareds = resultList.collect { case Right(shared) => shared } | ||
complete(StatusCodes.OK, shareds) | ||
} else { | ||
complete(HttpResponse(StatusCodes.InternalServerError, entity = errors.mkString(", "))) | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
} |