From 94cca449052d02606744b87da344a53f914aa344 Mon Sep 17 00:00:00 2001 From: Jean-Parra Date: Mon, 30 Oct 2023 13:09:07 -0500 Subject: [PATCH] Renombrar directorios y archivos --- .../controllers/DirectoryController.scala | 20 ++++++++++++++++++ .../example/controllers/FileController.scala | 21 +++++++++++++++++++ .../example/models/DirectoryAllModel.scala | 2 ++ .../scala/example/models/FileAllModel.scala | 2 ++ .../scala/example/routes/DirectoryRoute.scala | 13 +++++++++++- src/main/scala/example/routes/FileRoute.scala | 13 +++++++++++- 6 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/main/scala/example/controllers/DirectoryController.scala b/src/main/scala/example/controllers/DirectoryController.scala index 10959f2..1a1b1f5 100644 --- a/src/main/scala/example/controllers/DirectoryController.scala +++ b/src/main/scala/example/controllers/DirectoryController.scala @@ -305,5 +305,25 @@ class DirectoryController { } } } + def renombrarDirectorio(id: Int, nuevoNombre: String): Future[Either[String, String]] = { + Future { + try { + // Realizar la actualizaciĆ³n en la base de datos + val resultado = + sql"UPDATE directorios SET nombre = $nuevoNombre WHERE id = $id".update() + + if (resultado > 0) { + Right("Renombre exitoso") + + } else { + Left("No se pudo renombrar el directorio") + } + } catch { + case e: Exception => + println(s"Error interno del servidor: ${e.getMessage}") + Left("Error interno del servidor") + } + } + } } diff --git a/src/main/scala/example/controllers/FileController.scala b/src/main/scala/example/controllers/FileController.scala index 08506bc..23e7082 100644 --- a/src/main/scala/example/controllers/FileController.scala +++ b/src/main/scala/example/controllers/FileController.scala @@ -234,4 +234,25 @@ class FileController { } } } + + def renombrarArchivo(id: Int, nuevoNombre: String): Future[Either[String, String]] = { + Future { + try { + // Realizar la actualizaciĆ³n en la base de datos + val resultado = + sql"UPDATE archivos SET nombre = $nuevoNombre WHERE id = $id".update() + + if (resultado > 0) { + Right("Renombre exitoso") + + } else { + Left("No se pudo renombrar el archivo") + } + } catch { + case e: Exception => + println(s"Error interno del servidor: ${e.getMessage}") + Left("Error interno del servidor") + } + } + } } diff --git a/src/main/scala/example/models/DirectoryAllModel.scala b/src/main/scala/example/models/DirectoryAllModel.scala index 2bf9af5..5600533 100644 --- a/src/main/scala/example/models/DirectoryAllModel.scala +++ b/src/main/scala/example/models/DirectoryAllModel.scala @@ -23,3 +23,5 @@ case class SubDirectoryCreateModel( ) case class Directorysearch(usuario_id: Int, padre_id: Int) + +case class DirectoryRenameModel(id: Int, nuevoNombre: String) diff --git a/src/main/scala/example/models/FileAllModel.scala b/src/main/scala/example/models/FileAllModel.scala index 64e0448..2782090 100644 --- a/src/main/scala/example/models/FileAllModel.scala +++ b/src/main/scala/example/models/FileAllModel.scala @@ -25,3 +25,5 @@ case class FileCreateModel( directorio_id: Int, respaldo_id: Int ) + +case class FileRenameModel(id: Int, nuevoNombre: String) diff --git a/src/main/scala/example/routes/DirectoryRoute.scala b/src/main/scala/example/routes/DirectoryRoute.scala index 432eaa1..88455cd 100644 --- a/src/main/scala/example/routes/DirectoryRoute.scala +++ b/src/main/scala/example/routes/DirectoryRoute.scala @@ -6,7 +6,7 @@ import controllers.DirectoryController import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._ import io.circe.generic.auto._ import akka.http.scaladsl.model.{HttpResponse, StatusCodes} -import models.{DirectoryModel, DirectoryCreateModel, SubDirectoryCreateModel, Directorysearch} +import models.{DirectoryModel, DirectoryCreateModel, SubDirectoryCreateModel, Directorysearch, DirectoryRenameModel} import scala.concurrent.Future import com.typesafe.config.ConfigFactory @@ -129,6 +129,17 @@ class DirectoryRoute(directoryController: DirectoryController) { } } } + } ~ path("rename") { + put { + entity(as[DirectoryRenameModel]) { directory => + val result: Future[Either[String, String]] = + directoryController.renombrarDirectorio(directory.id, directory.nuevoNombre) + onSuccess(result) { + case Right(newDirectory) => complete(StatusCodes.Created, newDirectory) + case Left(errorMessage) => complete(HttpResponse(StatusCodes.InternalServerError, entity = errorMessage)) + } + } + } } } diff --git a/src/main/scala/example/routes/FileRoute.scala b/src/main/scala/example/routes/FileRoute.scala index bf7e35c..25212f3 100644 --- a/src/main/scala/example/routes/FileRoute.scala +++ b/src/main/scala/example/routes/FileRoute.scala @@ -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, FileRenameModel} import scala.concurrent.Future class FileRoute(fileController: FileController) { @@ -108,6 +108,17 @@ class FileRoute(fileController: FileController) { } } + } ~ path("rename") { + put { + entity(as[FileRenameModel]) { file => + val result: Future[Either[String, String]] = + fileController.renombrarArchivo(file.id, file.nuevoNombre) + onSuccess(result) { + case Right(newFile) => complete(StatusCodes.Created, newFile) + case Left(errorMessage) => complete(HttpResponse(StatusCodes.InternalServerError, entity = errorMessage)) + } + } + } } } }