Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

Commit

Permalink
Merge branch 'dev' into feat/delete-file
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbaronc authored Oct 22, 2023
2 parents a8222c7 + d936167 commit 292cb67
Show file tree
Hide file tree
Showing 11 changed files with 319 additions and 13 deletions.
18 changes: 9 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# [0.12.0](https://github.com/hawks-atlanta/metadata-scala/compare/v0.11.1...v0.12.0) (2023-10-22)


### Features

* Unshare file ([#89](https://github.com/hawks-atlanta/metadata-scala/issues/89)) ([be58b4a](https://github.com/hawks-atlanta/metadata-scala/commit/be58b4ad112ebe141f88781e8ea7d04713f1bd7d))



## [0.11.1](https://github.com/hawks-atlanta/metadata-scala/compare/v0.11.0...v0.11.1) (2023-10-14)


Expand Down Expand Up @@ -34,12 +43,3 @@



## [0.10.2](https://github.com/hawks-atlanta/metadata-scala/compare/v0.10.1...v0.10.2) (2023-09-29)


### Bug Fixes

* Ignore ready state validations for directories ([#73](https://github.com/hawks-atlanta/metadata-scala/issues/73)) ([fa5467f](https://github.com/hawks-atlanta/metadata-scala/commit/fa5467f86bda9312a6dad474bbdd3f5360a875c9))



2 changes: 1 addition & 1 deletion docs/bruno/shared-with-who/shared-with-who.bru
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ meta {
}

get {
url: {{BASE_URL}}/files/shared_with_who/22a7c6ca-8e57-46d2-9977-43dcbfcac760
url: {{BASE_URL}}/files/shared_with_who/76c5a635-08c4-46c9-89ef-89c120e2e55f
body: none
auth: none
}
17 changes: 17 additions & 0 deletions docs/bruno/unshare-file/unshare-file.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
meta {
name: unshare-file
type: http
seq: 1
}

post {
url: {{BASE_URL}}/files/unshare/5c8e5e9d-7d82-450f-9fe3-43fc73aa5a39/76c5a635-08c4-46c9-89ef-89c120e2e55f
body: json
auth: none
}

body:json {
{
"otherUserUUID": "e1139bb6-d291-4170-8fb0-94dd787fa84b"
}
}
27 changes: 27 additions & 0 deletions src/main/scala/files_metadata/application/FilesMetaUseCases.scala
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,33 @@ class FilesMetaUseCases {
repository.updateFileParent( fileUUID, newParentUUID )
}


def unShareFile(
ownerUUID: UUID,
fileUUID: UUID,
otherUserUUID: UUID
): Unit = {
val fileMeta = repository.getFileMeta( fileUUID )

if (fileMeta.ownerUuid != ownerUUID) {
throw DomainExceptions.FileNotOwnedException(
"You don't own the file"
)
}
if (ownerUUID == otherUserUUID) {
throw DomainExceptions.FileNotOwnedException(
"You cannot un-share a file with yourself"
)
}

if (!repository.isFileDirectlySharedWithUser( fileUUID, otherUserUUID )) {
throw DomainExceptions.FileAlreadySharedException(
"The file is not shared with the given user"
)
}

repository.unShareFile( fileUUID, otherUserUUID )
}
def deleteFile(
ownerUUID: UUID,
fileUUID: UUID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ trait FilesMetaRepository {
def updateFileName( fileUUID: UUID, newName: String ): Unit

def updateFileParent( fileUUID: UUID, parentUUID: Option[UUID] ): Unit
def unShareFile( fileUUID: UUID, userUUID: UUID ): Unit

// --- Delete ---
def deleteFileMeta( uuid: UUID ): Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,37 @@ class FilesMetaPostgresRepository extends FilesMetaRepository {
}
}


override def unShareFile( fileUUID: UUID, userUUID: UUID ): Unit = {
val connection: Connection = pool.getConnection()
connection.setAutoCommit( false )

try {
val shareStatement = connection.prepareStatement(
"DELETE FROM shared_files WHERE file_uuid =? AND user_uuid =?"
)
shareStatement.setObject( 1, fileUUID )
shareStatement.setObject( 2, userUUID )
shareStatement.executeUpdate()

val checkSharedStatement = connection.prepareStatement(
"SELECT * FROM shared_files WHERE file_uuid = ?"
)
checkSharedStatement.setObject( 1, fileUUID )
val resultSet = checkSharedStatement.executeQuery()
if (!resultSet.next()) {
val updateStatement = connection.prepareStatement(
"UPDATE files SET is_shared = false WHERE uuid = ?"
)
updateStatement.setObject( 1, fileUUID )
updateStatement.executeUpdate()
}

connection.commit()
} finally {
connection.close()
}
}
override def deleteFileMeta( uuid: UUID ): Unit = {
val connection: Connection = pool.getConnection()
try {
Expand All @@ -610,7 +641,6 @@ class FilesMetaPostgresRepository extends FilesMetaRepository {
connection.close()
}
}

override def deleteDirectoryMeta( uuid: UUID ): Unit = {
val connection: Connection = pool.getConnection()
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,50 @@ class MetadataControllers {
}
}


def UnShareFileController(
request: cask.Request,
ownerUUID: String,
fileUUID: String
): cask.Response[Obj] = {
try {
val decoded: ShareReqSchema = read[ShareReqSchema](
request.text()
)

val isOwnerUUIDValid = CommonValidator.validateUUID( ownerUUID )
val isFileUUIDValid = CommonValidator.validateUUID( fileUUID )

val validationRule: Validator[ShareReqSchema] =
ShareReqSchema.shareSchemaValidator
val validationResult = validate[ShareReqSchema]( decoded )(
validationRule
)
if (!isOwnerUUIDValid || !isFileUUIDValid || validationResult.isFailure) {
return cask.Response(
ujson.Obj(
"error" -> true,
"message" -> "Fields validation failed"
),
statusCode = 400
)
}


useCases.unShareFile(
ownerUUID = UUID.fromString( ownerUUID ),
fileUUID = UUID.fromString( fileUUID ),
otherUserUUID = UUID.fromString( decoded.otherUserUUID )
)

cask.Response(
None,
statusCode = 204
)
} catch {
case e: Exception => _handleException( e )
}
}
def DeleteFileController(
request: cask.Request,
ownerUUID: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,26 @@ case class MetadataRoutes() extends cask.Routes {
controllers.MoveFileController( request, userUUID, fileUUID )
)
}

private val unShareMetadataEndpoint =
s"$basePath/unshare/:ownerUUID/:fileUUID"

@cask.post( unShareMetadataEndpoint )
def UnShareMetadataHandler(
request: cask.Request,
ownerUUID: String,
fileUUID: String
): cask.Response[Obj] = {
StdoutLogger.logAndReturnEndpointResponse(
unShareMetadataEndpoint,
controllers.UnShareFileController( request, ownerUUID, fileUUID )
)
}
private val deleteMetadataEndpoint =
s"$basePath/delete/:ownerUUID/:fileUUID"
@cask.delete( deleteMetadataEndpoint )
def deleteMetadataHandler(
request: cask.Request,
request: cask.Request,
ownerUUID: String,
fileUUID: String
): cask.Response[Obj] = {
Expand Down
23 changes: 23 additions & 0 deletions src/test/scala/files_metadata/FilesTestsUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@ object FilesTestsUtils {
)
}

def UnShareFile(
ownerUUID: String,
fileUUID: String,
payload: util.HashMap[String, Any]
): Response = {
`given`()
.port( 8080 )
.contentType( "application/json" )
.body( payload )
.when()
.post(
s"${ UnShareFileTestsData.API_PREFIX }/$ownerUUID/$fileUUID"
)
}

def generateShareFilePayload(
otherUserUUID: UUID
): util.HashMap[String, Any] = {
Expand All @@ -116,6 +131,14 @@ object FilesTestsUtils {
shareFilePayload
}

def generateUnshareFilePayload(
otherUserUUID: UUID
): util.HashMap[String, Any] = {
val unShareFilePayload = new util.HashMap[String, Any]()
unShareFilePayload.put( "otherUserUUID", otherUserUUID.toString )
unShareFilePayload
}

def GetSharedWithUser( userUUID: String ): Response = {
`given`()
.port( 8080 )
Expand Down
Loading

0 comments on commit 292cb67

Please sign in to comment.