Skip to content

Commit 59734b4

Browse files
authored
Merge pull request #16 from bobocode-projects/GP-83_Migrate_account-rest-api_exercise_MAIN
Gp 83 migrate account rest api exercise main
2 parents bbbd710 + ac6b614 commit 59734b4

File tree

15 files changed

+494
-22
lines changed

15 files changed

+494
-22
lines changed

3-0-spring-framework/3-0-0-hello-spring-framework/pom.xml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,11 @@
1717
<artifactId>spring-context</artifactId>
1818
<version>5.2.12.RELEASE</version>
1919
</dependency>
20-
<dependency>
21-
<groupId>org.springframework</groupId>
22-
<artifactId>spring-test</artifactId>
23-
<version>5.2.12.RELEASE</version>
24-
</dependency>
2520
<dependency>
2621
<groupId>com.bobocode</groupId>
2722
<artifactId>spring-framework-exercises-util</artifactId>
2823
<version>1.0-SNAPSHOT</version>
2924
</dependency>
30-
<dependency>
31-
<groupId>org.hamcrest</groupId>
32-
<artifactId>hamcrest-all</artifactId>
33-
<version>1.3</version>
34-
<scope>test</scope>
35-
</dependency>
36-
<dependency>
37-
<groupId>org.slf4j</groupId>
38-
<artifactId>slf4j-simple</artifactId>
39-
<version>1.7.24</version>
40-
</dependency>
4125
<dependency>
4226
<groupId>com.bobocode</groupId>
4327
<artifactId>spring-framework-exercises-model</artifactId>

3-0-spring-framework/3-1-1-dispatcher-servlet-initializer/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
1313
<packaging>war</packaging>
1414

1515
<dependencies>
16-
<dependency>
17-
<groupId>org.springframework</groupId>
18-
<artifactId>spring-webmvc</artifactId>
19-
<version>5.2.12.RELEASE</version>
20-
</dependency>
2116
<dependency>
2217
<groupId>javax.servlet</groupId>
2318
<artifactId>javax.servlet-api</artifactId>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Account REST API exercise 💪
2+
Improve your *Spring MVC* configuration and rest mapping skills
3+
### Task
4+
This webapp provides a **simple REST API for `Account`**. The data is stored using in-memory fake DAO. Your job is to
5+
**configure Spring MVC application** and **implement AccountRestController**. In order to complete the task, please
6+
**follow the instructions in the *todo* section**
7+
8+
To verify your configuration, run `AccountRestControllerTest.java` :white_check_mark:
9+
10+
11+
### Pre-conditions ❗
12+
You're supposed to be familiar with *Spring MVC*
13+
14+
### How to start ❓
15+
* Just clone the repository and start implementing the **todo** section, verify your changes by running tests
16+
* If you don't have enough knowledge about this domain, check out the [links below](#related-materials-information_source)
17+
* Don't worry if you got stuck, checkout the **exercise/completed** branch and see the final implementation
18+
19+
### Related materials ℹ
20+
* todo
21+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>3-0-spring-framework</artifactId>
7+
<groupId>com.bobocode</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>3-2-1-account-rest-api</artifactId>
13+
<packaging>war</packaging>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>javax.servlet</groupId>
18+
<artifactId>javax.servlet-api</artifactId>
19+
<version>4.0.1</version>
20+
<scope>provided</scope>
21+
</dependency>
22+
<dependency>
23+
<groupId>com.bobocode</groupId>
24+
<artifactId>spring-framework-exercises-util</artifactId>
25+
<version>1.0-SNAPSHOT</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.jayway.jsonpath</groupId>
29+
<artifactId>json-path</artifactId>
30+
<version>2.3.0</version>
31+
<scope>test</scope>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.jayway.jsonpath</groupId>
35+
<artifactId>json-path-assert</artifactId>
36+
<version>2.3.0</version>
37+
<scope>test</scope>
38+
</dependency>
39+
</dependencies>
40+
41+
<build>
42+
<plugins>
43+
44+
<plugin>
45+
<groupId>org.apache.maven.plugins</groupId>
46+
<artifactId>maven-war-plugin</artifactId>
47+
<version>2.6</version>
48+
<configuration>
49+
<failOnMissingWebXml>false</failOnMissingWebXml>
50+
</configuration>
51+
</plugin>
52+
53+
</plugins>
54+
</build>
55+
56+
57+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.bobocode.config;
2+
3+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
4+
5+
public class AccountRestApiInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
6+
@Override
7+
protected Class<?>[] getRootConfigClasses() {
8+
return new Class[]{RootConfig.class};
9+
}
10+
11+
@Override
12+
protected Class<?>[] getServletConfigClasses() {
13+
return new Class[]{WebConfig.class};
14+
}
15+
16+
@Override
17+
protected String[] getServletMappings() {
18+
return new String[]{"/"};
19+
}
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.bobocode.config;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
5+
6+
/**
7+
* This class provides application root (non-web) configuration.
8+
* <p>
9+
* todo: 1. Mark this class as config
10+
* todo: 2. Enable component scanning for all packages in "com.bobocode" using annotation property "basePackages"
11+
* todo: 3. Exclude web related config and beans (ignore @{@link Controller}, ignore {@link EnableWebMvc})
12+
*/
13+
public class RootConfig {
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.bobocode.config;
2+
3+
/**
4+
* This class provides web (servlet) related configuration.
5+
* <p>
6+
* todo: 1. Mark this class as Spring config class
7+
* todo: 2. Enable web mvc using annotation
8+
* todo: 3. Enable component scanning for package "web" using annotation value
9+
*/
10+
public class WebConfig {
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.bobocode.dao;
2+
3+
import com.bobocode.model.Account;
4+
5+
import java.util.List;
6+
7+
public interface AccountDao {
8+
List<Account> findAll();
9+
10+
Account findById(long id);
11+
12+
Account save(Account account);
13+
14+
void remove(Account account);
15+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.bobocode.dao.impl;
2+
3+
import com.bobocode.dao.AccountDao;
4+
import com.bobocode.exception.EntityNotFountException;
5+
import com.bobocode.model.Account;
6+
7+
import java.util.ArrayList;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
/**
13+
* {@link AccountDao} implementation that is based on {@link java.util.HashMap}.
14+
* <p>
15+
* todo: 1. Configure a component with name "accountDao"
16+
*/
17+
public class InMemoryAccountDao implements AccountDao {
18+
private Map<Long, Account> accountMap = new HashMap<>();
19+
private long idSequence = 1L;
20+
21+
@Override
22+
public List<Account> findAll() {
23+
return new ArrayList<>(accountMap.values());
24+
}
25+
26+
@Override
27+
public Account findById(long id) {
28+
Account account = accountMap.get(id);
29+
if (account == null) {
30+
throw new EntityNotFountException(String.format("Cannot found account by id = %d", id));
31+
}
32+
return account;
33+
}
34+
35+
@Override
36+
public Account save(Account account) {
37+
if (account.getId() == null) {
38+
account.setId(idSequence++);
39+
}
40+
accountMap.put(account.getId(), account);
41+
return account;
42+
}
43+
44+
@Override
45+
public void remove(Account account) {
46+
accountMap.remove(account.getId());
47+
}
48+
49+
public void clear() {
50+
accountMap.clear();
51+
idSequence = 1L;
52+
}
53+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.bobocode.exception;
2+
3+
public class EntityNotFountException extends RuntimeException {
4+
public EntityNotFountException(String message) {
5+
super(message);
6+
}
7+
}

0 commit comments

Comments
 (0)