Skip to content
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.idea/
/target/
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions ReadMe.Md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
```
curl --location --request POST 'http://localhost:8080/idol/users' \
--header 'Content-Type: application/json' \
--data-raw '{
"todo_id": 1,
"real_name": "Real Name 1",
"idol_name": "Idol Name 1",
"address": "Sri Lanka",
"idol_status": "Completed"
}'
```

```
curl --location --request GET 'http://localhost:8080/idol/users/1'
```

```
curl --location --request PUT 'http://localhost:8080/idol/users' \
--header 'Content-Type: application/json' \
--data-raw '{
"id" : 1,
"todo_id": 1,
"real_name": "Real Name 1",
"idol_name": "Idol Name 1",
"address": "Sri Lanka",
"idol_status": "Completed"
}'
```

```
curl --location --request DELETE 'http://localhost:8080/idol/users/1'
```
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<!-- <version>2.6.0</version>-->
</dependency>
</dependencies>

<build>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/codejam/demo/constant/ErrorCodes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.codejam.demo.constant;

public interface ErrorCodes {

String LS001 = "LS001"; // Error code for data not found

String LS002 = "LS002"; // Error code for external call failure

String LS003 = "LS003"; // Error code for general error

}
54 changes: 54 additions & 0 deletions src/main/java/com/codejam/demo/controller/IdolController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.codejam.demo.controller;

import com.codejam.demo.dto.PersonalInformationDto;
import com.codejam.demo.dto.ResponseDto;
import com.codejam.demo.service.PersonalInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/idol")
public class IdolController {

private final PersonalInformationService personalInformationService;

@Autowired
public IdolController(PersonalInformationService personalInformationService) {
this.personalInformationService = personalInformationService;
}

@PostMapping(path = "/users", consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ResponseDto> save(@Valid @RequestBody PersonalInformationDto dto) {

return personalInformationService.save(dto);

}

@GetMapping(path = "users/{id}")
public ResponseEntity<PersonalInformationDto> get(
@PathVariable(value = "id") Integer id
) {
return personalInformationService.get(id);
}

@PutMapping(path = "/users", consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ResponseDto> updateById(@Valid @RequestBody PersonalInformationDto dto) {

return personalInformationService.update(dto);

}

@DeleteMapping(path = "users/{id}")
public ResponseEntity<ResponseDto> deleteById(
@PathVariable(value = "id") Integer id
) {
return personalInformationService.delete(id);
}

}
20 changes: 20 additions & 0 deletions src/main/java/com/codejam/demo/dto/ErrorResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.codejam.demo.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Builder
public class ErrorResponseDto {

private final String message;

private final String variable;

@JsonProperty(value = "error_code")
private final String errorCode;

}
51 changes: 51 additions & 0 deletions src/main/java/com/codejam/demo/dto/PersonalInformationDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.codejam.demo.dto;

import com.codejam.demo.model.PersonalInformationEntity;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;


@Getter
@Setter
public class PersonalInformationDto {

private Integer id;

@Min(value = 1, message = "todo id should not be less than 1")
@JsonProperty("todo_id")
@JsonInclude(JsonInclude.Include.NON_NULL)
private Integer todoId;

@Pattern(regexp = "^[^<>%$@#]*$", message = "Real Name should not be contains special characters")
@NotEmpty(message = "Real Name should not be empty")
@NotNull(message = "Real Name should not be null")
@JsonProperty("real_name")
private String realName;

@Pattern(regexp = "^[^<>%$@#]*$", message = "Idol Name should not be contains special characters")
@NotEmpty(message = "Idol Name should not be empty")
@NotNull(message = "Idol Name should not be null")
@JsonProperty("idol_name")
private String idolName;

@JsonProperty(value = "address")
private String dateTime;

@JsonProperty(value = "idol_status")
private String idolStatus;

public PersonalInformationEntity toEntity() {
PersonalInformationEntity entity = new PersonalInformationEntity();
BeanUtils.copyProperties(this, entity);
return entity;
}

}
13 changes: 13 additions & 0 deletions src/main/java/com/codejam/demo/dto/ResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.codejam.demo.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ResponseDto {

private String message;

}
17 changes: 17 additions & 0 deletions src/main/java/com/codejam/demo/dto/RevenueDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.codejam.demo.dto;

import lombok.Getter;
import lombok.Setter;


@Getter
@Setter
public class RevenueDto {

private Integer id;

private String monthlyRate;

private String dateTime;

}
19 changes: 19 additions & 0 deletions src/main/java/com/codejam/demo/dto/ScheduleDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.codejam.demo.dto;

import lombok.Getter;
import lombok.Setter;


@Getter
@Setter
public class ScheduleDto {

private Integer id;

private String venue;

private String eventName;

private String country;

}
25 changes: 25 additions & 0 deletions src/main/java/com/codejam/demo/dto/TodoDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.codejam.demo.dto;

import com.codejam.demo.model.TodoEntity;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.BeanUtils;

@Getter
@Setter
public class TodoDto {

private Integer userId;

private Integer id;

private String title;

private boolean completed;

public TodoEntity toEntity() {
TodoEntity entity = new TodoEntity();
BeanUtils.copyProperties(this, entity);
return entity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.codejam.demo.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class DataNotFoundException extends RuntimeException {

private final String message;

private final String variable;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.codejam.demo.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class DuplicateDataFoundException extends RuntimeException{

private final String message;

private final String variable;
}
50 changes: 50 additions & 0 deletions src/main/java/com/codejam/demo/exception/ErrorAdvice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.codejam.demo.exception;

import com.codejam.demo.constant.ErrorCodes;
import com.codejam.demo.dto.ErrorResponseDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@ControllerAdvice
public class ErrorAdvice extends ResponseEntityExceptionHandler {

@ExceptionHandler(DataNotFoundException.class)
public ResponseEntity<ErrorResponseDto> handleDataNotFoundException(DataNotFoundException ex) {

ErrorResponseDto errorResponseDto = ErrorResponseDto.builder()
.errorCode(ErrorCodes.LS001)
.message(ex.getMessage())
.variable(ex.getVariable()).build();

return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponseDto);

}

@ExceptionHandler(ExternalCallException.class)
public ResponseEntity<ErrorResponseDto> handleExternalCallException(ExternalCallException ex) {

ErrorResponseDto errorResponseDto = ErrorResponseDto.builder()
.errorCode(ErrorCodes.LS002)
.message(ex.getMessage())
.variable(ex.getVariable()).build();

return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponseDto);

}

@ExceptionHandler(GeneralException.class)
public ResponseEntity<ErrorResponseDto> handleGeneralException(GeneralException ex) {

ErrorResponseDto errorResponseDto = ErrorResponseDto.builder()
.errorCode(ErrorCodes.LS003)
.message(ex.getMessage())
.variable(ex.getVariable()).build();

return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponseDto);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.codejam.demo.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class ExternalCallException extends RuntimeException {

private final String message;

private final String variable;

}
16 changes: 16 additions & 0 deletions src/main/java/com/codejam/demo/exception/GeneralException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.codejam.demo.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class GeneralException extends RuntimeException {

private final String message;

private final String variable;

}
Loading