Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
@AllArgsConstructor
public class SpaceUpdateRequest {
private String title;

private Boolean isShared;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.gp_backend_data.space.domain.dto.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;

import java.time.LocalDateTime;
Expand All @@ -15,4 +16,7 @@ public class SpaceResponse {
private String title;
private LocalDateTime createdAt;
private LocalDateTime lastVisitedAt;
@JsonProperty("isShared")
private boolean isShared;
private UUID sharingId;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.example.gp_backend_data.space.domain.entity;

import com.example.gp_backend_data.utils.UUIDHelper;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;

import java.time.LocalDateTime;
import java.util.UUID;
Expand Down Expand Up @@ -36,8 +36,27 @@ public class Space {
@Column(name = "last_visited_at", nullable = false)
private LocalDateTime lastVisitedAt;

@Column(name = "is_shared", nullable = false)
@ColumnDefault("false")
@Builder.Default
private boolean isShared = false;

@Column(name = "sharing_id", nullable = true, unique = true)
private byte[] sharingId;

public void updateTitle(String title) {
this.title = title;
this.lastVisitedAt = LocalDateTime.now();
}

public void updateSharingStatusWithId(boolean isShared, UUIDHelper uuidHelper) {
this.isShared = isShared;
this.lastVisitedAt = LocalDateTime.now();

if (isShared && this.sharingId == null) {
this.sharingId = uuidHelper.convertUUIDToByteArray(UUID.randomUUID());
} else if (!isShared) {
this.sharingId = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public List<SpaceResponse> getSpaces(UUID userId, String sortBy){
.title(space.getTitle())
.createdAt(space.getCreatedAt())
.lastVisitedAt(space.getLastVisitedAt())
.isShared(space.isShared())
.sharingId(uuidHelper.convertByteArrayToUUID(space.getSharingId()))
.build())
.toList();
}
Expand All @@ -61,14 +63,24 @@ public SpaceResponse updateSpace(UUID userId, UUID spaceId, SpaceUpdateRequest r
Space space = spaceRepository.findBySpaceIdAndUserId(uuidHelper.convertUUIDToByteArray(spaceId), uuidHelper.convertUUIDToByteArray(userId))
.orElseThrow(() -> new RuntimeException("Space not found"));

space.updateTitle(request.getTitle());
if (request.getTitle() != null) {
space.updateTitle(request.getTitle());
}

if (request.getIsShared() != null) {
// `isShared` 여부에 따라 sharingId 자동 할당됨
space.updateSharingStatusWithId(request.getIsShared(), uuidHelper);
}

space = spaceRepository.saveAndFlush(space);

return SpaceResponse.builder()
.spaceId(uuidHelper.convertByteArrayToUUID(space.getSpaceId()))
.title(space.getTitle())
.createdAt(space.getCreatedAt())
.lastVisitedAt(space.getLastVisitedAt())
.isShared(space.isShared())
.sharingId(uuidHelper.convertByteArrayToUUID(space.getSharingId()))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@
@Component
public class UUIDHelper {
public byte[] convertUUIDToByteArray(UUID uuid) {
if (uuid == null) {
return null;
}

ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());
return bb.array();
}

public UUID convertByteArrayToUUID(byte[] bytes) {
if (bytes == null) {
return null;
}

ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
long high = byteBuffer.getLong();
long low = byteBuffer.getLong();
Expand Down
Loading