Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 필터 API를 구현한다 #451

Merged
merged 17 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions backend/src/docs/asciidoc/Filter.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
= Filter API 문서
:doctype: book
:icons: font
:source-highlighter: highlightjs
:toc: left
:toclevels: 3

== 등록된 필터를 모두 조회한다. (/filters)

=== Request

include::{snippets}/filter-controller-test/find_all_filters/request-headers.adoc[]
include::{snippets}/filter-controller-test/find_all_filters/http-request.adoc[]

=== Response

include::{snippets}/filter-controller-test/find_all_filters/response-fields.adoc[]
include::{snippets}/filter-controller-test/find_all_filters/http-response.adoc[]

== 필터를 등록한다. (/filters)

=== Request

include::{snippets}/filter-controller-test/add_filters/request-headers.adoc[]
include::{snippets}/filter-controller-test/add_filters/request-fields.adoc[]
include::{snippets}/filter-controller-test/add_filters/http-request.adoc[]

=== Response

include::{snippets}/filter-controller-test/add_filters/response-fields.adoc[]
include::{snippets}/filter-controller-test/add_filters/http-response.adoc[]


== 필터를 제거한다. (/filters/{filterName})

=== Request

include::{snippets}/filter-controller-test/delete_filter/request-headers.adoc[]
include::{snippets}/filter-controller-test/delete_filter/path-parameters.adoc[]
include::{snippets}/filter-controller-test/delete_filter/http-request.adoc[]

=== Response

include::{snippets}/filter-controller-test/delete_filter/http-response.adoc[]
1 change: 1 addition & 0 deletions backend/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ endif::[]
:sectlinks:

include::station.adoc[]
include::Filter.adoc[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.carffeine.carffeine.filter.controller;

import com.carffeine.carffeine.auth.controller.AuthMember;
import com.carffeine.carffeine.filter.dto.FiltersRequest;
import com.carffeine.carffeine.filter.dto.FiltersResponse;
import com.carffeine.carffeine.filter.service.FilterService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
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.RestController;

@RequiredArgsConstructor
@RequestMapping("/filters")
@RestController
public class FilterController {

private final FilterService filterService;

@GetMapping
public ResponseEntity<FiltersResponse> findAllFilters(@AuthMember Long memberId) {
return ResponseEntity.ok(FiltersResponse.from(filterService.findAllFilters(memberId)));
}

@PostMapping
public ResponseEntity<FiltersResponse> addFilters(@AuthMember Long memberId,
@RequestBody FiltersRequest filtersRequest) {
return ResponseEntity.status(HttpStatus.CREATED)
.body(filterService.addFilters(memberId, filtersRequest));
}

@DeleteMapping("/{filterName}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filter id 가 아니라 name 인 이유가 있나요?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 궁금합니다

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅎㅎ.. 반환값을 프론트와 맞춰서 id를 주지 않습니다 그래서 name으로 받고 있습니다

public ResponseEntity<Void> deleteFilter(@AuthMember Long memberId,
@PathVariable String filterName) {
filterService.deleteFilter(memberId, filterName);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.carffeine.carffeine.filter.domain;

import com.carffeine.carffeine.common.domain.BaseEntity;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@EqualsAndHashCode(of = {"id"}, callSuper = false)
@Table(name = "filter")
@Entity
public class Filter extends BaseEntity {

private static final String DECIMAL_SIGN = ".00";

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false, unique = true)
private String name;

@Enumerated(EnumType.STRING)
private FilterType filterType;

public static Filter of(String name, String filterType) {
FilterType type = FilterType.from(filterType);

if (type.isCapacities()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type==cFilterType.Capacity 를 써도 좋을 것 같아요

String capacity = makeCapacity(name);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name 이라는 변수가 뭔가 직관적이지 않은 것 같아요

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사실상 필터의 이름인데 capacitiy 값은 BigDecimal이라서 그런가봐요 ㅠㅠ
그래도 확장이나 전반적으로 생각하면 filter의 명이니 name이 좋을 것 같아서 냅두겠습니다!

return new Filter(null, capacity, type);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null을 제외한 생성자를 만드는 것은 어떤가요?

}

return new Filter(null, name, type);
}

private static String makeCapacity(String capacity) {
if (capacity.endsWith(DECIMAL_SIGN)) {
return capacity;
}

return capacity + DECIMAL_SIGN;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.carffeine.carffeine.filter.domain;

import org.springframework.data.repository.Repository;

import java.util.List;
import java.util.Optional;

public interface FilterRepository extends Repository<Filter, Long> {

List<Filter> findAll();

<S extends Filter> List<S> saveAll(Iterable<S> filters);

Optional<Filter> findByName(String name);

void deleteByName(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.carffeine.carffeine.filter.domain;

import com.carffeine.carffeine.filter.exception.FilterException;
import com.carffeine.carffeine.filter.exception.FilterExceptionType;

import java.util.Arrays;

public enum FilterType {

COMPANIES("companies"),
CAPACITIES("capacities"),
CONNECTOR_TYPES("connectorTypes");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type에 복수형을 하신 이유가 있나요?


private final String name;

FilterType(String name) {
this.name = name;
}

public static FilterType from(String name) {
return Arrays.stream(values())
.filter(it -> it.getName().equals(name))
.findAny()
.orElseThrow(() -> new FilterException(FilterExceptionType.FILTER_NOT_FOUND));
}

public boolean isCompanies() {
return this.name.equals(COMPANIES.name);
}

public boolean isConnectorTypes() {
return this.name.equals(CONNECTOR_TYPES.name);
}

public boolean isCapacities() {
return this.name.equals(CAPACITIES.name);
}

public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.carffeine.carffeine.filter.dto;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

패키지 구조가 저희 컨벤션이랑 조금 다른 것 같은데 특별한 이유가 있을까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확인하지 못했네요
현재 구조에서 원시 타입을 service에서 controller로 넘기는 구조입니다.

따라서 service 패키지 안으로 넣겠습니다


import java.util.List;

public record FiltersRequest(
List<String> companies,
List<String> capacities,
List<String> connectorTypes
) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request를 이렇게 받으시는 이유가 있나요?

{[
   {
    "type": "company",
    "name": "GA1,
    },
    {
       "type": capacity,
       "name": 50
    }
]}

이런식으로 받으면 안되나요..?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그렇게된다면 filter Type이 추가되더라도 해당 request의 필드를 추가하거나 변경하지 않아도 될 것 같아서요

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋은 방법 감사합니다!

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.carffeine.carffeine.filter.dto;

import com.carffeine.carffeine.filter.domain.Filter;

import java.util.List;
import java.util.stream.Collectors;

public record FiltersResponse(
List<String> companies,
List<String> capacities,
List<String> connectorTypes
) {

public static FiltersResponse from(List<Filter> filters) {
List<String> companies = filters.stream()
.filter(it -> it.getFilterType().isCompanies())
.map(Filter::getName)
.toList();

List<String> capacities = filters.stream()
.filter(it -> it.getFilterType().isCapacities())
.map(Filter::getName)
.toList();

List<String> connectorTypes = filters.stream()
.filter(it -> it.getFilterType().isConnectorTypes())
.map(Filter::getName)
.toList();

return new FiltersResponse(
companies,
capacities,
connectorTypes
);
}

public static FiltersResponse from(List<Filter> companiesFilter, List<Filter> capacitiesFilter, List<Filter> connectorTypesFilter) {
List<String> companies = companiesFilter.stream()
.map(Filter::getName)
.collect(Collectors.toList());

List<String> capacities = capacitiesFilter.stream()
.map(Filter::getName)
.collect(Collectors.toList());

List<String> connectorTypes = connectorTypesFilter.stream()
.map(Filter::getName)
.collect(Collectors.toList());

return new FiltersResponse(
companies,
capacities,
connectorTypes
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.carffeine.carffeine.filter.exception;

import com.carffeine.carffeine.common.exception.BaseException;
import com.carffeine.carffeine.common.exception.ExceptionType;

public class FilterException extends BaseException {

public FilterException(ExceptionType exceptionType) {
super(exceptionType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.carffeine.carffeine.filter.exception;

import com.carffeine.carffeine.common.exception.ExceptionType;
import com.carffeine.carffeine.common.exception.Status;

public enum FilterExceptionType implements ExceptionType {

FILTER_NOT_FOUND(Status.NOT_FOUND, 1001, "해당 필터를 찾을 수 없습니다.");

private final Status status;
private final int exceptionCode;
private final String message;

FilterExceptionType(Status status, int exceptionCode, String message) {
this.status = status;
this.exceptionCode = exceptionCode;
this.message = message;
}

@Override
public Status status() {
return status;
}

@Override
public int exceptionCode() {
return exceptionCode;
}

@Override
public String message() {
return message;
}
}