Skip to content

Commit

Permalink
Renombrar directorios y archivos
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Parra committed Oct 30, 2023
1 parent ad4b176 commit 94cca44
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/main/scala/example/controllers/DirectoryController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}

}
21 changes: 21 additions & 0 deletions src/main/scala/example/controllers/FileController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
}
2 changes: 2 additions & 0 deletions src/main/scala/example/models/DirectoryAllModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ case class SubDirectoryCreateModel(
)

case class Directorysearch(usuario_id: Int, padre_id: Int)

case class DirectoryRenameModel(id: Int, nuevoNombre: String)
2 changes: 2 additions & 0 deletions src/main/scala/example/models/FileAllModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ case class FileCreateModel(
directorio_id: Int,
respaldo_id: Int
)

case class FileRenameModel(id: Int, nuevoNombre: String)
13 changes: 12 additions & 1 deletion src/main/scala/example/routes/DirectoryRoute.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
}
}
}
}

}
Expand Down
13 changes: 12 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, FileRenameModel}
import scala.concurrent.Future

class FileRoute(fileController: FileController) {
Expand Down Expand Up @@ -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))
}
}
}
}
}
}

0 comments on commit 94cca44

Please sign in to comment.