Skip to content
Open

Task #36

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
17 changes: 0 additions & 17 deletions 0-0-intro/0-0-1-welcome-to-java-web-exercises/README.MD

This file was deleted.

59 changes: 0 additions & 59 deletions 0-0-intro/0-0-1-welcome-to-java-web-exercises/pom.xml

This file was deleted.

This file was deleted.

Binary file not shown.

This file was deleted.

18 changes: 0 additions & 18 deletions 0-0-intro/pom.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private ClientUtil() {
*/
@SneakyThrows
public static Socket openSocket(String host, int port) {
throw new ExerciseNotCompletedException(); // todo: implement according to javadoc and verify by ClientUtilTest
return new Socket(host, port);
}

/**
Expand Down Expand Up @@ -62,6 +62,10 @@ public static String readMessage(BufferedReader reader) {
*/
@SneakyThrows
public static void writeToSocket(String message, Socket socket) {
throw new ExerciseNotCompletedException(); // todo: implement according to javadoc and verify by ClientUtilTest
OutputStream outputStream = socket.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));

writer.write(message);
writer.flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static String getLocalHost() {
*/
@SneakyThrows
public static ServerSocket createServerSocket(int port) {
throw new ExerciseNotCompletedException(); // todo: implement according to javadoc and verify by ServerUtilTest
return new ServerSocket(port);
}

/**
Expand All @@ -52,7 +52,7 @@ public static ServerSocket createServerSocket(int port) {
*/
@SneakyThrows
public static Socket acceptClientSocket(ServerSocket serverSocket) {
throw new ExerciseNotCompletedException(); // todo: implement according to javadoc and verify by ServerUtilTest
return serverSocket.accept();
}

/**
Expand All @@ -66,7 +66,9 @@ public static Socket acceptClientSocket(ServerSocket serverSocket) {
*/
@SneakyThrows
public static String readMessageFromSocket(Socket socket) {
throw new ExerciseNotCompletedException(); // todo: implement according to javadoc and verify by ServerUtilTest
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

return bufferedReader.readLine();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.bobocode.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDate;

@WebServlet("/date")
public class DateServlet extends HttpServlet {

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter out = resp.getWriter();
out.println(LocalDate.now());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.bobocode.config;

import com.bobocode.TestDataGenerator;
import com.bobocode.dao.FakeAccountDao;
import com.bobocode.service.AccountService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
* This class specifies application context configuration. It tells Spring to scan "dao" and "service" packages in order
Expand All @@ -10,7 +15,12 @@
* It also explicitly configures a bean of {@link TestDataGenerator} called "dataGenerator". This beans will be injected
* into {@link com.bobocode.dao.FakeAccountDao} in order to generate some fake accounts.
*/
@Configuration
@ComponentScan(basePackages = {"com.bobocode.dao", "com.bobocode.service"})
public class ApplicationConfig {
// todo: configure application context according to javadoc by following tests in ApplicationConfigTest
// todo: verify final implementation by running ApplicationContextTest

@Bean
public TestDataGenerator dataGenerator() {
return new TestDataGenerator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.bobocode.TestDataGenerator;
import com.bobocode.model.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Stream;
Expand All @@ -16,9 +18,11 @@
* Its bean is called "accountDao". And it uses constructor with explicit autowired annotation in order to inject
* {@link TestDataGenerator} instance.
*/
@Component("accountDao")
public class FakeAccountDao implements AccountDao {
private List<Account> accounts;

@Autowired
public FakeAccountDao(TestDataGenerator testDataGenerator) {
this.accounts = Stream.generate(testDataGenerator::generateAccount)
.limit(20)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.bobocode.dao.AccountDao;
import com.bobocode.model.Account;
import org.springframework.stereotype.Service;

import static java.util.Comparator.comparing;

Expand All @@ -12,6 +13,7 @@
* Since it's a service that should be added to the application context, it is marked as Spring service. It order to get
* {@link AccountDao} instances, it uses implicit constructor-based injection.
*/
@Service
public class AccountService {
private final AccountDao accountDao;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.bobocode.mvc.api;

import com.bobocode.mvc.data.Notes;
import com.bobocode.mvc.model.Note;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* This controller provides a very simple REST API for Notes. It implements two endpoints that allow you to add
Expand All @@ -18,9 +22,21 @@
* Spring MVC was used to build the whole application including front-end. So the controllers were connected to the views
* via models, like in {@link com.bobocode.mvc.controller.NoteController}
*/
@RestController
@RequestMapping("/api/notes")
@RequiredArgsConstructor
public class NoteRestController {
private final Notes notes;

// TODO: implement controller methods according to the javadoc verify your impl using NoteRestControllerTest
@GetMapping
public List<Note> getNotes() {
return notes.getAll();
}

@PostMapping
public void addNote(
@RequestBody Note note
) {
notes.add(note);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package com.bobocode.mvc.controller;

import com.bobocode.mvc.data.Notes;
import com.bobocode.mvc.model.Note;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
* {@link NoteController} is a typical controller that powers Spring MVC Notes application. This application provides
Expand All @@ -26,10 +34,24 @@
* back-end applications. The back-end app, will only need to provide data and don't care about view. In that case
* the same controller will look like {@link com.bobocode.mvc.api.NoteRestController}
*/
@Controller
@RequiredArgsConstructor
@RequestMapping("/notes")
public class NoteController {
private final Notes notes;

// TODO: implement controller methods according to the javadoc and verify your impl using NoteControllerTest
@GetMapping
public String notesPage(Model model) {
List<Note> noteList = notes.getAll();
model.addAttribute("noteList", noteList);

return "notes";
}

@PostMapping
public String addNote(Note note) {
notes.add(note);
return "redirect:/notes";
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.bobocode.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

Expand All @@ -11,9 +11,12 @@
* (containing middle-tire services, datasource, etc.).
* The configuration must exclude the web layer of the application.
* <p>
* todo: mark this class as config
* todo: enable component scanning for all packages in "com.bobocode"
* todo: ignore all web related config and beans (ignore @{@link Controller}, ignore {@link EnableWebMvc}) using exclude filter
*/
@Configuration
@ComponentScan(basePackages = "com.bobocode",
excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class),
@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Controller.class),
})
public class RootConfig {
}
Loading