Skip to content

Commit

Permalink
Respaldos
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Parra committed Oct 23, 2023
1 parent 9d43f5e commit 12ef0d5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/main/scala/example/controllers/FileController.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package controllers

import scalikejdbc._
import models.{FileModel, FileReportModel}
import models.{FileModel, FileReportModel, respaldoModel}

import io.circe._
import io.circe.generic.auto._
Expand Down Expand Up @@ -220,5 +220,22 @@ class FileController {
}
}
}
def guardarRespaldo(respaldo: respaldoModel): Future[Either[String, String]] = {
Future {
try {
// Inserta el nuevo respaldo en la base de datos
sql"""
INSERT INTO respaldos (ruta, archivo_id, nodo_id, usuario_id)
VALUES (${respaldo.ruta}, ${respaldo.archivoId}, ${respaldo.nodoId}, ${respaldo.usuarioId})
""".update()

Right("Respaldo guardado correctamente")
} catch {
case e: Exception =>
println(s"Error interno del servidor: ${e.getMessage}")
Left("Error interno del servidor")
}
}
}

}
7 changes: 7 additions & 0 deletions src/main/scala/example/models/FileAllModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ case class FileCreateModel(
nodo_id: Int,
directorio_id: Int
)

case class respaldoModel(
ruta: String,
archivoId: Int,
nodoId: Int,
usuarioId: Int
)
14 changes: 13 additions & 1 deletion src/main/scala/example/routes/FileRoute.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import controllers.FileController
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._
import io.circe.generic.auto._
import akka.http.scaladsl.model.{HttpResponse, StatusCodes}
import models.{FileModel, FileReportModel, FileMoveModel, FileCreateModel}
import models.{FileModel, FileReportModel, FileMoveModel, FileCreateModel, respaldoModel}
import scala.concurrent.Future

class FileRoute(fileController: FileController) {
Expand Down Expand Up @@ -99,6 +99,18 @@ class FileRoute(fileController: FileController) {
}

}
} ~
path("respaldos") {
post {
entity(as[respaldoModel]) { respaldo =>
val result: Future[Either[String, String]] =
fileController.guardarRespaldo(respaldo)
onSuccess(result) {
case Right(message) => complete(StatusCodes.Created, message)
case Left(errorMessage) => complete(HttpResponse(StatusCodes.InternalServerError, entity = errorMessage))
}
}
}
}
}
}

0 comments on commit 12ef0d5

Please sign in to comment.