Skip to content

Commit 0cd3597

Browse files
REST update: return 201 CREATED for contact endpoint
Signed-off-by: vimal-tech-starter <varnam2311@gmail.com>
1 parent 2abe6e2 commit 0cd3597

6 files changed

Lines changed: 35 additions & 5 deletions

File tree

src/main/java/com/vimaltech/contactapi/config/CorsConfig.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.vimaltech.contactapi.config;
22

3+
import org.jspecify.annotations.NonNull;
34
import org.springframework.context.annotation.Bean;
45
import org.springframework.context.annotation.Configuration;
56
import org.springframework.web.servlet.config.annotation.CorsRegistry;
@@ -12,10 +13,10 @@ public class CorsConfig {
1213
public WebMvcConfigurer corsConfigurer() {
1314
return new WebMvcConfigurer() {
1415
@Override
15-
public void addCorsMappings(CorsRegistry registry) {
16+
public void addCorsMappings(@NonNull CorsRegistry registry) {
1617
registry.addMapping("/**")
1718
.allowedOrigins("https://vimaltech.dev")
18-
.allowedMethods("GET", "POST", "PUT", "DELETE")
19+
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
1920
.allowedHeaders("*");
2021
}
2122
};

src/main/java/com/vimaltech/contactapi/controller/ContactController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.vimaltech.contactapi.service.ContactService;
66
import jakarta.validation.Valid;
77
import lombok.RequiredArgsConstructor;
8+
import org.springframework.http.HttpStatus;
89
import org.springframework.http.ResponseEntity;
910
import org.springframework.web.bind.annotation.*;
1011

@@ -13,7 +14,6 @@
1314
@RestController
1415
@RequestMapping("/api/v1/contact")
1516
@RequiredArgsConstructor
16-
@CrossOrigin(origins = "https://vimaltech.dev")
1717
public class ContactController {
1818

1919
private final ContactService contactService;
@@ -30,6 +30,6 @@ public ResponseEntity<ApiResponse> submitContact(
3030
LocalDateTime.now()
3131
);
3232

33-
return ResponseEntity.ok(response);
33+
return ResponseEntity.status(HttpStatus.CREATED).body(response);
3434
}
3535
}

src/main/java/com/vimaltech/contactapi/dto/ContactRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public record ContactRequest(
1313
@NotBlank(message = "Email is required")
1414
String email,
1515

16+
@Size(max = 255, message = "Subject cannot exceed 255 characters")
1617
String subject,
1718

1819
@NotBlank(message = "Message is required")

src/main/java/com/vimaltech/contactapi/entity/ContactInquiry.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@ public class ContactInquiry {
2424
@Column(nullable = false)
2525
private String email;
2626

27+
@Column(length = 255)
2728
private String subject;
2829

2930
@Column(nullable = false, length = 1000)
3031
private String message;
3132

32-
@Column(name = "created_at", nullable = false)
33+
@Column(name = "created_at", nullable = false, updatable = false)
3334
private LocalDateTime createdAt;
35+
36+
@PrePersist
37+
public void prePersist() {
38+
this.createdAt = LocalDateTime.now();
39+
}
3440
}

src/main/java/com/vimaltech/contactapi/exception/GlobalExceptionHandler.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,16 @@ public ResponseEntity<ApiResponse> handleValidationException(
2929

3030
return ResponseEntity.badRequest().body(response);
3131
}
32+
33+
@ExceptionHandler(Exception.class)
34+
public ResponseEntity<ApiResponse> handleGenericException(Exception ex) {
35+
36+
ApiResponse response = new ApiResponse(
37+
false,
38+
"Something went wrong. Please try again later.",
39+
LocalDateTime.now()
40+
);
41+
42+
return ResponseEntity.internalServerError().body(response);
43+
}
3244
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.vimaltech.contactapi.dto;
2+
3+
import java.time.LocalDateTime;
4+
5+
public record ApiResponse(
6+
boolean success,
7+
String message,
8+
LocalDateTime timestamp
9+
) {
10+
}

0 commit comments

Comments
 (0)