Skip to content

Commit

Permalink
Merge branch 'develop' into refactor/#127
Browse files Browse the repository at this point in the history
  • Loading branch information
seheonnn authored Feb 6, 2024
2 parents c7aedd6 + b849e17 commit f1858f1
Show file tree
Hide file tree
Showing 16 changed files with 202 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public ApiResponse<List<BookmarkGetResponse>> getRecentBookmark(
if (sortStatus == BookmarkStatus.RECENT) {
return ApiResponse.onSuccess(bookmarkService.getRecentBookmark(authOrganization));
}

if (sortStatus == BookmarkStatus.SAVED) {
return ApiResponse.onSuccess(bookmarkService.getSavedBookmark(authOrganization));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public record BookmarkGetResponse(
Long announcementId,
String announcementTitle,
LocalDateTime createdAt,
Long savedCount
Long saveCount
) {

public static BookmarkGetResponse from(Bookmark bookmark) {
Expand All @@ -22,7 +22,7 @@ public static BookmarkGetResponse from(Bookmark bookmark) {
.announcementId(bookmark.getAnnouncement().getId())
.announcementTitle(bookmark.getAnnouncement().getTitle())
.createdAt(bookmark.getCreatedAt())
.savedCount(bookmark.getSavedCount())
.saveCount(bookmark.getSaveCount())
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public class Bookmark extends BaseEntity {
@Column(name = "bookmark_id")
private Long id;

@Column(name = "saved_count", nullable = false)
private long savedCount;
@Column(name = "save_count", nullable = false)
private long saveCount;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "organization_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
Expand All @@ -47,9 +47,10 @@ public class Bookmark extends BaseEntity {
@JoinColumn(name = "announcement_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private Announcement announcement;

public void increaseSavedCount() { this.savedCount++; }
public void decreaseSavedCount() {
if (this.savedCount > 0) {
this.savedCount--;
public void increaseSaveCount() { this.saveCount++; }

public void decreaseSaveCount() {
if (this.saveCount > 0) {
this.saveCount--;
} }
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public interface BookmarkRepository extends JpaRepository<Bookmark, Long> {

Optional<Bookmark> findByOrganizationAndAnnouncement(Organization organization, Announcement announcement);
List<BookmarkGetResponse> findByOrganizationOrderByCreatedAtDesc(Organization organization);
List<BookmarkGetResponse> findByOrganizationOrderBySavedCountDesc(Organization organization);

List<BookmarkGetResponse> findByOrganizationOrderBySaveCountDesc(Organization organization);
}

Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public BookmarkToggleResponse bookmarkToggle(Organization organization, Bookmark

if (existingBookmark != null) {
bookmarkRepository.delete(existingBookmark);
existingBookmark.decreaseSavedCount();
existingBookmark.decreaseSaveCount();
return BookmarkToggleResponse.from(existingBookmark, false); // 이미 북마크가 되어있는 경우 취소
} else {
final Bookmark bookmark = bookmarkRepository.save(request.toEntity(organization, announcement));
bookmark.increaseSavedCount();
bookmark.increaseSaveCount();
return BookmarkToggleResponse.from(bookmark, true); // 북마크가 안되어있는 경우 등록
}
}
Expand All @@ -51,6 +51,8 @@ public List<BookmarkGetResponse> getRecentBookmark(Organization organization) {
}

public List<BookmarkGetResponse> getSavedBookmark(Organization organization) {
return bookmarkRepository.findByOrganizationOrderBySavedCountDesc(organization);
return bookmarkRepository.findByOrganizationOrderBySaveCountDesc(organization);
}


}
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package com.sponus.sponusbe.domain.report.controller;

import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.sponus.sponusbe.domain.report.dto.ReportRequest;
import com.sponus.sponusbe.domain.report.dto.ReportResponse;
import com.sponus.sponusbe.auth.annotation.AuthOrganization;
import com.sponus.sponusbe.domain.organization.entity.Organization;
import com.sponus.sponusbe.domain.report.dto.ReportCreateRequest;
import com.sponus.sponusbe.domain.report.dto.ReportCreateResponse;
import com.sponus.sponusbe.domain.report.service.ReportQueryService;
import com.sponus.sponusbe.domain.report.service.ReportService;
import com.sponus.sponusbe.global.common.ApiResponse;
Expand All @@ -25,19 +31,22 @@ public class ReportController {
private final ReportService reportService;
private final ReportQueryService reportQueryService;

@PostMapping
public ApiResponse<ReportResponse> createReport(@Valid @RequestBody ReportRequest request) {
return ApiResponse.onSuccess(reportService.createReport(request));
@PostMapping(consumes = "multipart/form-data")
public ApiResponse<ReportCreateResponse> createReport(
@AuthOrganization Organization authOrganization,
@RequestPart("request") @Valid ReportCreateRequest request,
@RequestPart(value = "images") List<MultipartFile> images) {
return ApiResponse.onSuccess(reportService.createReport(authOrganization, request, images));
}

@PatchMapping("/{reportId}")
public ApiResponse<ReportResponse> updateReport(@PathVariable Long reportId,
@Valid @RequestBody ReportRequest request) {
public ApiResponse<ReportCreateResponse> updateReport(@PathVariable Long reportId,
@Valid @RequestBody ReportCreateRequest request) {
return ApiResponse.onSuccess(reportService.updateReport(reportId, request));
}

@GetMapping("/{reportId}")
public ApiResponse<ReportResponse> readReport(@PathVariable Long reportId) {
public ApiResponse<ReportCreateResponse> readReport(@PathVariable Long reportId) {
return ApiResponse.onSuccess(reportQueryService.readReport(reportId));
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.sponus.sponusbe.domain.report.dto;

import lombok.Builder;

@Builder
public record ReportAttachmentResponse(
Long id,
String name,
String url
) {
}

Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
package com.sponus.sponusbe.domain.report.dto;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.multipart.MultipartFile;

import com.sponus.sponusbe.domain.organization.entity.Organization;
import com.sponus.sponusbe.domain.report.entity.Report;

import jakarta.validation.constraints.NotNull;

public record ReportRequest(
public record ReportCreateRequest(
@NotNull(message = "[ERROR] 보고서 제목 입력은 필수 입니다.")
String title,

@NotNull(message = "[ERROR] 보고서 내용 입력은 필수 입니다.")
String content,

@NotNull(message = "[ERROR] 첨부 파일은 필수입니다.")
List<MultipartFile> files
String content
) {

public Report toEntity() {
public Report toEntity(Organization writer) {
return Report.builder()
.writer(writer)
.title(title)
.content(content)
.reportAttachments(new ArrayList<>())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.sponus.sponusbe.domain.report.dto;

import com.sponus.sponusbe.domain.report.entity.Report;

import lombok.Builder;

@Builder
public record ReportCreateResponse(
Long id,
Long writerId,
String title,
String content
) {

public static ReportCreateResponse from(Report report) {
return ReportCreateResponse.builder()
.id(report.getId())
.writerId(report.getWriter().getId())
.title(report.getTitle())
.content(report.getContent())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.sponus.sponusbe.domain.report.dto;

import com.sponus.sponusbe.domain.report.entity.ReportImage;

import lombok.Builder;

@Builder
public record ReportImageResponse(
Long id,
String name,
String url
) {

public static ReportImageResponse from(ReportImage image) {
return ReportImageResponse.builder()
.id(image.getId())
.name(image.getName())
.url(image.getUrl())
.build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package com.sponus.sponusbe.domain.report.entity;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import com.sponus.sponusbe.domain.organization.entity.Organization;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.ConstraintMode;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import lombok.AccessLevel;
Expand Down Expand Up @@ -42,9 +51,23 @@ public class Report {
@OneToMany(mappedBy = "report")
private List<ReportAttachment> reportAttachments;

// TODO : 첨부파일 추가
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "organization_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private Organization writer;

@Builder.Default
@OneToMany(mappedBy = "report", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ReportImage> reportImages = new ArrayList<>();

public void update(String title, String content) {
this.title = title == null ? this.title : title;
this.content = content == null ? this.content : content;
}

public void updateImages(List<ReportImage> images) {
if (images != null) {
this.reportImages.clear();
this.reportImages.addAll(images);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.sponus.sponusbe.domain.report.entity;

import jakarta.persistence.Column;
import jakarta.persistence.ConstraintMode;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
@Entity
@Table(name = "report_image")
public class ReportImage {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "image_id")
private Long id;

@Column(name = "image_name", nullable = false)
private String name;

@Column(name = "image_url", nullable = false)
private String url;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "report_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private Report report;

public void setReport(Report report) {
if (this.report != null) {
this.report.getReportImages().remove(this);
}
this.report = report;
report.getReportImages().add(this);
}
}
Loading

0 comments on commit f1858f1

Please sign in to comment.