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
7 changes: 5 additions & 2 deletions src/main/java/hello/controller/TopicController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import hello.model.Topic;
import hello.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -29,8 +30,10 @@ public List<Topic> getAllTopics() {
* @return
*/
@RequestMapping("/topic/{id}")
public Topic getTopicWithID(@PathVariable String id) {
return topicService.getTopicWithId(id);
public ResponseEntity<Topic> getTopicWithID(@PathVariable String id) {
return topicService.getTopicWithId(id)
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build());
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/java/hello/declaration/CustomPredicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Use as a Functional Interface
* @param <T>
*/
@FunctionalInterface
public interface CustomPredicate <T>{
boolean test(T t);
}
43 changes: 1 addition & 42 deletions src/main/java/hello/model/Customer.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,4 @@
package hello.model;

public class Customer {
private long id;
private String firstName, lastName;

@Override
public String toString() {
return "Customer{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}

public Customer(long id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}
public record Customer(long id, String firstName, String lastName) {
}
19 changes: 1 addition & 18 deletions src/main/java/hello/model/Greeting.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
package hello.model;

public class Greeting {
private final long id;
private final String content;

public Greeting(long id, String content) {
this.id = id;
this.content = content;
}


public long getId() {

return id;
}

public String getContent() {
return content;
}
public record Greeting(long id, String content) {
}
32 changes: 1 addition & 31 deletions src/main/java/hello/model/Quote.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,4 @@
package hello.model;

public class Quote {
private String type;
private Value value;

@Override
public String toString() {
return "Quote{" +
"type='" + type + '\'' +
", value=" + value +
'}';
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public Value getValue() {
return value;
}

public void setValue(Value value) {
this.value = value;
}

public Quote() {

}
public record Quote(String type, Value value) {
}
41 changes: 1 addition & 40 deletions src/main/java/hello/model/Topic.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,4 @@
package hello.model;


public class Topic {
private String id;
private String subjectName;
private String subjectDescription;

public Topic() {
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getSubjectName() {
return subjectName;
}

public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
}

public String getSubjectDescription() {
return subjectDescription;
}

public void setSubjectDescription(String subjectDescription) {
this.subjectDescription = subjectDescription;
}

public Topic(String id, String subjectName, String subjectDescription) {
super();
this.id = id;
this.subjectName = subjectName;
this.subjectDescription = subjectDescription;
}

public record Topic(String id, String subjectName, String subjectDescription) {
}
31 changes: 1 addition & 30 deletions src/main/java/hello/model/Value.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,4 @@
package hello.model;

public class Value {
private Long id;
private String quote;

public Value() {
}

@Override
public String toString() {
return "Value{" +
"id=" + id +
", quote='" + quote + '\'' +
'}';
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getQuote() {
return quote;
}

public void setQuote(String quote) {
this.quote = quote;
}
public record Value(Long id, String quote) {
}
20 changes: 9 additions & 11 deletions src/main/java/hello/service/TopicService.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public List<Topic> getAllTopics() {
* @param id
* @return
*/
public Topic getTopicWithId(String id) {
return topics.stream().filter(topic -> topic.getId().equals(id)).findFirst().get();
public Optional<Topic> getTopicWithId(String id) {
return topics.stream().filter(topic -> topic.id().equals(id)).findFirst();
}

public void addTopic(Topic topic) {
Expand All @@ -50,7 +50,7 @@ public void addTopic(Topic topic) {
* @param topic
*/
public void updateTopic(String id, Topic topic) {
OptionalInt indexOfElement = IntStream.range(0, topics.size()).filter(index -> id.equals(topics.get(index).getId())).findFirst();
OptionalInt indexOfElement = IntStream.range(0, topics.size()).filter(index -> id.equals(topics.get(index).id())).findFirst();
if (indexOfElement.isPresent()) topics.set(indexOfElement.getAsInt(), topic);
}

Expand All @@ -60,7 +60,7 @@ public void updateTopic(String id, Topic topic) {
* @param id
*/
public void deleteTopic(String id) {
topics.removeIf(topic -> topic.getId().equals(id));
topics.removeIf(topic -> topic.id().equals(id));
}

/**
Expand All @@ -70,7 +70,7 @@ public void deleteTopic(String id) {
* @return
*/
public List<Topic> filterMinimumLengthForId(Integer minLength) {
return printTopicsWithPredicate(topics, topic -> topic.getId().length() > minLength);
return printTopicsWithPredicate(topics, topic -> topic.id().length() > minLength);
}


Expand All @@ -96,7 +96,7 @@ private static List<Topic> printTopicsWithPredicate(List<Topic> topicList, Custo
* @return
*/
public List<Topic> sortTopicsWithID() {
topics.sort(Comparator.comparing(Topic::getId));
topics.sort(Comparator.comparing(Topic::id));
return topics;
}

Expand All @@ -107,7 +107,7 @@ public List<Topic> sortTopicsWithID() {
* @return
*/
public String returnAllTopicIDWithStringSlicing() {
List<String> topicIds = topics.stream().map(topic -> topic.getId()).collect(Collectors.toList());
List<String> topicIds = topics.stream().map(Topic::id).toList();
return String.join(":", topicIds);
}

Expand Down Expand Up @@ -147,13 +147,11 @@ public String splitAllIdWithColonSelectIDWithJavaKeywordThenSortThenJoin(String
*/
public String findIdHavingCharacter() {
Pattern pattern = Pattern.compile(".*g.*");
Object[] topicIdObjectList = topics.stream().map(topic -> topic.getId()).collect(Collectors.toList()).toArray();

String[] topicIdList = Arrays.stream(topicIdObjectList).toArray(String[]::new);
String[] topicIdList = topics.stream().map(Topic::id).toArray(String[]::new);

return Stream.of(topicIdList)
.filter(pattern.asPredicate())
.collect(Collectors.toList())
.toList()
.toString();
}

Expand Down
Loading