-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
8 changed files
with
273 additions
and
0 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
77 changes: 77 additions & 0 deletions
77
app/src/main/kotlin/com/akagiyui/drive/controller/SharingController.kt
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.akagiyui.drive.controller | ||
|
||
import com.akagiyui.drive.component.CurrentUser | ||
import com.akagiyui.drive.entity.User | ||
import com.akagiyui.drive.model.response.sharing.SharingResponse | ||
import com.akagiyui.drive.model.response.sharing.toResponse | ||
import com.akagiyui.drive.service.SharingService | ||
import org.springframework.web.bind.annotation.* | ||
|
||
/** | ||
* 分享资源 控制器 | ||
* @author AkagiYui | ||
*/ | ||
@RestController | ||
@RequestMapping("/share") | ||
class SharingController(private val sharingService: SharingService) { | ||
|
||
/** | ||
* 获取分享列表 | ||
* | ||
* @param user 用户 | ||
* @return 分享列表 | ||
*/ | ||
@GetMapping | ||
fun getSharingList(@CurrentUser user: User): List<SharingResponse> { | ||
return sharingService.list(user.id).toResponse() | ||
} | ||
|
||
/** | ||
* 创建分享 | ||
* | ||
* @param id 文件ID | ||
* @param user 用户 | ||
* @return 分享信息 | ||
*/ | ||
@PostMapping("/{id}") | ||
fun createSharing(@PathVariable id: String, @CurrentUser user: User): SharingResponse { | ||
return SharingResponse(sharingService.createSharing(user, id)) | ||
} | ||
|
||
/** | ||
* 删除分享 | ||
* | ||
* @param id 分享ID | ||
* @param user 用户 | ||
*/ | ||
@DeleteMapping("/{id}") | ||
fun deleteSharing(@PathVariable id: String, @CurrentUser user: User) { | ||
sharingService.deleteSharing(user.id, id) | ||
} | ||
|
||
/** | ||
* 获取分享 | ||
* | ||
* @param id 分享ID | ||
* @param user 用户 | ||
* @return 分享信息 | ||
*/ | ||
@GetMapping("/{id}") | ||
fun getSharing(@PathVariable id: String, @CurrentUser user: User?): SharingResponse? { | ||
val sharing = sharingService.getSharing(id) ?: return null | ||
return SharingResponse(sharing) | ||
} | ||
|
||
/** | ||
* 根据用户文件ID获取分享 | ||
* | ||
* @param id 文件ID | ||
* @param user 用户 | ||
* @return 分享信息 | ||
*/ | ||
@GetMapping("/file/{id}") | ||
fun getSharingByUserFileId(@PathVariable id: String, @CurrentUser user: User): SharingResponse? { | ||
val sharing = sharingService.getSharingByUserFile(id, user.id) ?: return null | ||
return SharingResponse(sharing) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.akagiyui.drive.entity | ||
|
||
import com.akagiyui.common.entity.BaseEntity | ||
import jakarta.persistence.* | ||
import org.hibernate.annotations.DynamicInsert | ||
|
||
/** | ||
* 分享实体 | ||
* @author AkagiYui | ||
*/ | ||
@Entity | ||
@Table | ||
@DynamicInsert | ||
class Sharing : BaseEntity() { | ||
|
||
/** | ||
* 分享者 | ||
*/ | ||
@ManyToOne(fetch = FetchType.EAGER) | ||
@JoinColumn(name = "user_id", nullable = false) | ||
lateinit var user: User | ||
|
||
/** | ||
* 密码 | ||
*/ | ||
@Column | ||
var password: String? = null | ||
|
||
/** | ||
* 文件 | ||
*/ | ||
@OneToOne(fetch = FetchType.EAGER) | ||
lateinit var file: UserFile | ||
} |
33 changes: 33 additions & 0 deletions
33
app/src/main/kotlin/com/akagiyui/drive/model/response/sharing/SharingResponse.kt
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.akagiyui.drive.model.response.sharing | ||
|
||
import com.akagiyui.drive.entity.Sharing | ||
|
||
/** | ||
* 分享信息响应 | ||
* @author AkagiYui | ||
*/ | ||
|
||
data class SharingResponse( | ||
val id: String, | ||
val filename: String, | ||
val createTime: Long, | ||
val password: String?, | ||
) { | ||
|
||
constructor(sharing: Sharing) : this( | ||
id = sharing.id, | ||
filename = sharing.file.name, | ||
createTime = sharing.createTime.time, | ||
password = sharing.password | ||
) | ||
|
||
} | ||
|
||
/** | ||
* 从分享列表转换 | ||
* | ||
* @return 分享信息响应列表 | ||
*/ | ||
fun List<Sharing>.toResponse(): List<SharingResponse> { | ||
return this.map { SharingResponse(it) } | ||
} |
33 changes: 33 additions & 0 deletions
33
app/src/main/kotlin/com/akagiyui/drive/repository/SharingRepository.kt
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.akagiyui.drive.repository | ||
|
||
import com.akagiyui.drive.entity.Sharing | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor | ||
|
||
/** | ||
* 分享资源 操作接口 | ||
* @author AkagiYui | ||
*/ | ||
interface SharingRepository : JpaRepository<Sharing, String>, JpaSpecificationExecutor<Sharing> { | ||
|
||
/** | ||
* 根据用户ID查找分享资源 | ||
* @param userId 用户ID | ||
*/ | ||
fun findByUserId(userId: String): List<Sharing> | ||
|
||
/** | ||
* 根据用户ID、ID查找分享资源 | ||
* @param userId 用户ID | ||
* @param id 分享ID | ||
*/ | ||
fun findByUserIdAndId(userId: String, id: String): Sharing? | ||
|
||
/** | ||
* 根据用户ID、文件ID查找分享资源 | ||
* @param userId 用户ID | ||
* @param userFileId 用户文件ID | ||
*/ | ||
fun findByUserIdAndFileId(userId: String, userFileId: String): Sharing? | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
app/src/main/kotlin/com/akagiyui/drive/service/SharingService.kt
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.akagiyui.drive.service | ||
|
||
import com.akagiyui.drive.entity.Sharing | ||
import com.akagiyui.drive.entity.User | ||
|
||
/** | ||
* 分享资源 服务 | ||
* @author AkagiYui | ||
*/ | ||
interface SharingService { | ||
|
||
/** | ||
* 创建分享 | ||
* @param user 用户 | ||
* @param userFileId 用户文件ID | ||
*/ | ||
fun createSharing(user: User, userFileId: String): Sharing | ||
|
||
/** | ||
* 删除分享 | ||
* @param userId 用户ID | ||
* @param sharingId 分享ID | ||
*/ | ||
fun deleteSharing(userId: String, sharingId: String) | ||
|
||
/** | ||
* 获取分享列表 | ||
* @param userId 用户ID | ||
*/ | ||
fun list(userId: String): List<Sharing> | ||
|
||
/** | ||
* 获取分享 | ||
* @param sharingId 分享ID | ||
*/ | ||
fun getSharing(sharingId: String): Sharing? | ||
|
||
/** | ||
* 根据用户文件ID获取分享 | ||
* @param userFileId 用户文件ID | ||
* @param userId 用户ID | ||
*/ | ||
fun getSharingByUserFile(userFileId: String, userId: String): Sharing? | ||
} |
48 changes: 48 additions & 0 deletions
48
app/src/main/kotlin/com/akagiyui/drive/service/impl/SharingServiceImpl.kt
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.akagiyui.drive.service.impl | ||
|
||
import com.akagiyui.common.utils.BASE_LOWER_CASE | ||
import com.akagiyui.common.utils.BASE_NUMBER | ||
import com.akagiyui.common.utils.random | ||
import com.akagiyui.drive.entity.Sharing | ||
import com.akagiyui.drive.entity.User | ||
import com.akagiyui.drive.repository.SharingRepository | ||
import com.akagiyui.drive.service.SharingService | ||
import com.akagiyui.drive.service.UserFileService | ||
import org.springframework.stereotype.Service | ||
|
||
/** | ||
* 分享资源 服务实现 | ||
* @author AkagiYui | ||
*/ | ||
@Service | ||
class SharingServiceImpl( | ||
private val repository: SharingRepository, | ||
private val userFileService: UserFileService, | ||
) : SharingService { | ||
override fun createSharing(user: User, userFileId: String): Sharing { | ||
val password = String.random("${String.BASE_LOWER_CASE}${String.BASE_NUMBER}", 4) | ||
val sharing = Sharing().apply { | ||
this.user = user | ||
this.file = userFileService.getUserFileById(user.id, userFileId) | ||
this.password = password | ||
} | ||
return repository.save(sharing) | ||
} | ||
|
||
override fun getSharingByUserFile(userFileId: String, userId: String): Sharing? { | ||
return repository.findByUserIdAndFileId(userId, userFileId) | ||
} | ||
|
||
override fun deleteSharing(userId: String, sharingId: String) { | ||
val sharing = repository.findByUserIdAndId(userId, sharingId) ?: throw RuntimeException("分享不存在") | ||
repository.delete(sharing) | ||
} | ||
|
||
override fun list(userId: String): List<Sharing> { | ||
return repository.findByUserId(userId) | ||
} | ||
|
||
override fun getSharing(sharingId: String): Sharing? { | ||
return repository.findById(sharingId).orElse(null) | ||
} | ||
} |