Skip to content

Commit 089a807

Browse files
committed
comment and tests
1 parent 9894c39 commit 089a807

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1164
-48
lines changed
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package com.csaba79coder.littersnap;
22

3-
import org.springframework.boot.ApplicationArguments;
4-
import org.springframework.boot.ApplicationRunner;
53
import org.springframework.boot.SpringApplication;
64
import org.springframework.boot.autoconfigure.SpringBootApplication;
75

6+
/**
7+
* The main class of the application.
8+
*/
89
@SpringBootApplication
9-
public class LitterSnapApplication implements ApplicationRunner {
10+
public class LitterSnapApplication {
1011

12+
/**
13+
* The main method of the application.
14+
*
15+
* @param args The command line arguments.
16+
*/
1117
public static void main(String[] args) {
1218
SpringApplication.run(LitterSnapApplication.class, args);
1319
}
14-
15-
@Override
16-
public void run(ApplicationArguments args) throws Exception {
17-
18-
}
1920
}

src/main/java/com/csaba79coder/littersnap/config/SecurityConfiguration.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,31 @@
1212
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
1313
import org.springframework.security.web.SecurityFilterChain;
1414

15+
/**
16+
* This is a temporary solution to provide a security configuration for the application.
17+
*/
1518
@Configuration
1619
@EnableWebSecurity
1720
public class SecurityConfiguration {
1821

22+
/**
23+
* This is a temporary solution to provide a password encoder for the application.
24+
* @return
25+
* @see <a href="https://docs.spring.io/spring-security/site/docs/current/reference/html5/#servlet-hello-bcrypt">Spring Security documentation</a>
26+
* @see <a href="https://www.baeldung.com/spring-security-registration-password-encoding-bcrypt">Spring Security Registration Password Encoding with Bcrypt</a>
27+
* @see <a href="https://www.baeldung.com/spring-security-registration-i-password-encoding-bcrypt">Spring Security Registration Password Encoding with Bcrypt</a>
28+
*/
1929
@Bean
2030
public PasswordEncoder encoder() {
2131
return new BCryptPasswordEncoder();
2232
}
2333

34+
/**
35+
* This is a temporary solution to provide a security filter chain for the application.
36+
* @param http
37+
* @return
38+
* @throws Exception
39+
*/
2440
@Bean
2541
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
2642
http
@@ -36,25 +52,13 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
3652
.and()
3753
.csrf().disable(); // Disable CSRF protection
3854
return http.build();
39-
40-
/*
41-
this disables all authentication and authorization for the moment!
42-
http
43-
.authorizeRequests()
44-
.anyRequest().permitAll() // Allow access to all endpoints
45-
.and()
46-
.formLogin()
47-
.disable() // Disable form-based login
48-
.and()
49-
.logout()
50-
.disable() // Disable logout functionality
51-
.and()
52-
.csrf()
53-
.disable(); // Disable CSRF protection
54-
return http.build();
55-
*/
5655
}
5756

57+
/**
58+
* This is a temporary solution to provide a user for the application.
59+
* This is not a good practice, but it is enough for the moment.
60+
* <p>
61+
*/
5862
@Bean
5963
public UserDetailsService users() {
6064
UserDetails user = User.builder()

src/main/java/com/csaba79coder/littersnap/controller/EmailController.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,27 @@
1010
import org.springframework.web.bind.annotation.RequestMapping;
1111
import org.springframework.web.bind.annotation.RestController;
1212

13+
/**
14+
* EmailController
15+
*/
1316
@RestController
1417
@RequiredArgsConstructor
1518
@RequestMapping("/api/auth")
1619
public class EmailController {
1720

21+
/**
22+
* The litterSnapEmailSenderService field.
23+
* <p>
24+
* This field is used to send emails.
25+
* </p>
26+
*/
1827
private final LitterSnapEmailSenderServiceImpl litterSnapEmailSenderService;
1928

29+
/**
30+
* This method is used to send a test email.
31+
* @param emailRequest the email request
32+
* @return the response entity
33+
*/
2034
@PostMapping(value = "/admin/email/test",
2135
produces = "application/json")
2236
public ResponseEntity sendTestEmail(@RequestBody EmailRequest emailRequest) {

src/main/java/com/csaba79coder/littersnap/controller/LitterController.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@
77
import lombok.RequiredArgsConstructor;
88
import org.springframework.http.HttpStatus;
99
import org.springframework.http.ResponseEntity;
10-
import org.springframework.web.bind.annotation.*;
10+
import org.springframework.web.bind.annotation.DeleteMapping;
11+
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.ModelAttribute;
13+
import org.springframework.web.bind.annotation.PathVariable;
14+
import org.springframework.web.bind.annotation.PostMapping;
15+
import org.springframework.web.bind.annotation.PutMapping;
16+
import org.springframework.web.bind.annotation.RequestBody;
17+
import org.springframework.web.bind.annotation.RequestMapping;
18+
import org.springframework.web.bind.annotation.RequestParam;
19+
import org.springframework.web.bind.annotation.RestController;
1120
import org.springframework.web.multipart.MultipartFile;
1221

1322
import java.util.List;
@@ -28,9 +37,11 @@ public List<LitterModel> renderAllLitters() {
2837
return litterService.findAllLitters();
2938
}
3039

31-
@PostMapping("/admin/litters")
32-
public ResponseEntity<LitterModel> addNewLitter(@RequestBody LitterCreateOrModifyModel litterModel,
33-
@RequestBody Address address,
40+
@PostMapping(value = "/admin/litters",
41+
consumes = "multipart/form-data",
42+
produces = "application/json")
43+
public ResponseEntity<LitterModel> addNewLitter(@ModelAttribute LitterCreateOrModifyModel litterModel,
44+
@ModelAttribute Address address,
3445
@RequestParam("file") MultipartFile file) {
3546
return ResponseEntity.status(201).body(litterService.addNewLitter(litterModel, address, file));
3647
}
@@ -45,7 +56,9 @@ public ResponseEntity<LitterModel> renderLitterById(@PathVariable("id") UUID id)
4556
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
4657
}
4758

48-
@PutMapping("/admin/litters/modify/{id}")
59+
@PutMapping(value = "/admin/litters/modify/{id}",
60+
consumes = "multipart/form-data",
61+
produces = "application/json")
4962
public ResponseEntity<LitterModel> updateExistingLitter(@PathVariable("id") UUID id,
5063
@RequestBody LitterCreateOrModifyModel model) {
5164
return ResponseEntity.status(200).body(litterService.modifyAnExistingLitter(id, model));

src/main/java/com/csaba79coder/littersnap/exception/ControllerExceptionHandler.java

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.csaba79coder.littersnap.value.ErrorCode;
44
import jakarta.mail.SendFailedException;
5-
import org.modelmapper.ValidationException;
65
import org.springframework.http.HttpStatus;
76
import org.springframework.http.ResponseEntity;
87
import org.springframework.web.bind.annotation.ControllerAdvice;
@@ -18,34 +17,75 @@
1817
import static com.csaba79coder.littersnap.value.ErrorCode.LS_004;
1918
import static com.csaba79coder.littersnap.value.ErrorCode.LS_005;
2019

20+
/**
21+
* This class contains the exception handlers for the application.
22+
*/
2123
@ControllerAdvice
2224
public class ControllerExceptionHandler {
2325

26+
/**
27+
* Handles NoSuchElementException.
28+
*
29+
* @param ex the exception to handle
30+
* @return the response entity with the error message
31+
*/
2432
@ExceptionHandler(value = {NoSuchElementException.class})
2533
public ResponseEntity<Object> handleNoSuchElementException(NoSuchElementException ex) {
2634
return new ResponseEntity<>(responseBodyWithMessage(LS_001, ex.getMessage()), HttpStatus.NOT_FOUND);
2735
}
2836

37+
/**
38+
* Handles InputMismatchException.
39+
*
40+
* @param ex the exception to handle
41+
* @return the response entity with the error message
42+
*/
2943
@ExceptionHandler(value = {InputMismatchException.class})
3044
public ResponseEntity<Object> handleInvalidInputException(InputMismatchException ex) {
3145
return new ResponseEntity<>(responseBodyWithMessage(LS_002, ex.getMessage()), HttpStatus.BAD_REQUEST);
3246
}
3347

34-
@ExceptionHandler(value = {ValidationException.class})
35-
public ResponseEntity<Object> handleInvalidPasswordOrUsernameException(ValidationException ex) {
36-
return new ResponseEntity<>(responseBodyWithMessage(LS_003, ex.getMessage()), HttpStatus.BAD_REQUEST);
48+
/**
49+
* Handles MyValidationException with a single string message.
50+
*
51+
* @param ex the exception to handle
52+
* @return the response entity with the error message
53+
*/
54+
@ExceptionHandler(value = {MyValidationException.class})
55+
public ResponseEntity<Object> handleInvalidPasswordOrUsernameException(MyValidationException ex) {
56+
String message = ex.getMessage();
57+
return new ResponseEntity<>(responseBodyWithMessage(LS_003, message), HttpStatus.BAD_REQUEST);
3758
}
3859

60+
/**
61+
* Handles SendFailedException.
62+
*
63+
* @param ex the exception to handle
64+
* @return the response entity with the error message
65+
*/
3966
@ExceptionHandler(value = {SendFailedException.class})
4067
public ResponseEntity<Object> handleSendFailedException(SendFailedException ex) {
4168
return new ResponseEntity<>(responseBodyWithMessage(LS_004, ex.getMessage()), HttpStatus.BAD_REQUEST);
4269
}
4370

71+
/**
72+
* Handles IllegalArgumentException.
73+
*
74+
* @param ex the exception to handle
75+
* @return the response entity with the error message
76+
*/
4477
@ExceptionHandler(value = {IllegalArgumentException.class})
4578
public ResponseEntity<Object> handleIllegalArgumentException(IllegalArgumentException ex) {
4679
return new ResponseEntity<>(responseBodyWithMessage(LS_005, ex.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
4780
}
4881

82+
/**
83+
* Creates the response body with the error message.
84+
*
85+
* @param code the error code
86+
* @param message the error message
87+
* @return the response body with the error message
88+
*/
4989
private String responseBodyWithMessage(ErrorCode code, String message) {
5090
return Map.of(code, message).toString();
5191
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.csaba79coder.littersnap.exception;
2+
3+
/**
4+
* This class contains a custom exception for validation errors.
5+
*/
6+
public class MyValidationException extends RuntimeException {
7+
8+
/**
9+
* Constructor for MyValidationException.
10+
*
11+
* @param message the message to be displayed
12+
* checking email and password if it is valid.
13+
*/
14+
public MyValidationException(String message) {
15+
super(message);
16+
}
17+
}

src/main/java/com/csaba79coder/littersnap/model/address/dto/AddressModel.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,25 @@
22

33
import lombok.*;
44

5+
/**
6+
* This class contains the address model.
7+
*/
58
@Getter
69
@Setter
710
@AllArgsConstructor
811
@NoArgsConstructor
912
public class AddressModel {
1013

14+
/**
15+
* The address model fields.
16+
* <p>
17+
* id: the address id
18+
* firstLine: the first line of the address
19+
* postCode: the post code of the address
20+
* city: the city of the address
21+
* country: the country of the address
22+
* </p>
23+
*/
1124
private String firstLine;
1225
private String postCode;
1326
private String city;

src/main/java/com/csaba79coder/littersnap/model/address/entity/Address.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
import java.util.List;
1212

13+
/**
14+
* This class contains the address entity.
15+
*/
1316
@Entity
1417
@Table(name = "address")
1518
@Getter
@@ -18,6 +21,24 @@
1821
@NoArgsConstructor
1922
public class Address extends Auditable {
2023

24+
/**
25+
* The address entity fields.
26+
* <p>
27+
* id: the address id
28+
* firstLine: the first line of the address
29+
* postCode: the post code of the address
30+
* city: the city of the address
31+
* country: the country of the address
32+
* </p>
33+
* Extends Auditable class, that extends Identifier class, which contains the following fields:
34+
* <p>
35+
* createdAt: the date when the entity was created
36+
* updatedAt: the date when the entity was last modified
37+
* createdBy: the user who created the entity
38+
* updatedBy: the user who last modified the entity
39+
* id: the address id (UUID) - this is the primary key and auto-generated
40+
* </p>
41+
*/
2142
@Column(name = "first_line")
2243
private String firstLine;
2344

@@ -30,6 +51,12 @@ public class Address extends Auditable {
3051
@Column(name = "country")
3152
private String country;
3253

54+
/**
55+
* The address entity relationships.
56+
* <p>
57+
* litters: the list of litters associated with the address
58+
* </p>
59+
*/
3360
@OneToMany(mappedBy = "address")
3461
private List<Litter> litters;
3562
}

src/main/java/com/csaba79coder/littersnap/model/address/persitence/AddressRepository.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,21 @@
77
import java.util.Optional;
88
import java.util.UUID;
99

10+
/**
11+
* This class contains the address repository.
12+
*/
1013
@Repository
1114
public interface AddressRepository extends JpaRepository<Address, UUID> {
1215

16+
/**
17+
* This method finds an address by city, country, post code and first line.
18+
*
19+
* @param city the city of the address
20+
* @param country the country of the address
21+
* @param postCode the post code of the address
22+
* @param firstLine the first line of the address
23+
* @return the address
24+
*/
1325
Optional<Address> findAddressByCityContainingIgnoreCaseAndCountryContainingIgnoreCaseAndPostCodeAndFirstLineContainsIgnoreCase(
1426
String city, String country, String postCode, String firstLine);
1527
}

src/main/java/com/csaba79coder/littersnap/model/base/entity/Auditable.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,25 @@
99
import java.time.LocalDateTime;
1010
import java.util.UUID;
1111

12+
/**
13+
* This class contains the auditable entity.
14+
*/
1215
@MappedSuperclass
1316
@Getter
1417
@Setter
1518
public class Auditable extends Identifier {
1619

20+
/**
21+
* The auditable entity fields.
22+
* <p>
23+
* createdAt: the date and time when the entity was created
24+
* updatedAt: the date and time when the entity was updated
25+
* createdBy: the user who created the entity
26+
* updatedBy: the user who updated the entity
27+
* </p>
28+
* extended from Identifier:
29+
* id: the auditable id
30+
*/
1731
@CreationTimestamp
1832
@Column(name = "created_at", nullable = false)
1933
private LocalDateTime createdAt = LocalDateTime.now();

0 commit comments

Comments
 (0)