-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e978fc
commit a2c1797
Showing
12 changed files
with
127 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 0 additions & 32 deletions
32
src/main/java/com/libraryman_api/newsletter/EmailService1.java
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
src/main/java/com/libraryman_api/newsletter/MailConfig.java
This file was deleted.
Oops, something went wrong.
59 changes: 52 additions & 7 deletions
59
src/main/java/com/libraryman_api/newsletter/NewsletterController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,71 @@ | ||
package com.libraryman_api.newsletter; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/newsletter") | ||
@RequestMapping("/api/newsletter") | ||
public class NewsletterController { | ||
|
||
private final NewsletterService newsletterService; | ||
|
||
@Autowired | ||
public NewsletterController(NewsletterService newsletterService) { | ||
this.newsletterService = newsletterService; | ||
} | ||
|
||
// Subscribe Endpoint | ||
@PostMapping("/subscribe") | ||
public String subscribe(@RequestParam String email) { | ||
return newsletterService.subscribe(email); | ||
public ResponseEntity<String> subscribe(@RequestParam String email) { | ||
try { | ||
String result = newsletterService.subscribe(email); | ||
|
||
switch (result) { | ||
case "Invalid email format.": | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result); // 400 Bad Request | ||
|
||
case "Email is already subscribed.": | ||
return ResponseEntity.status(HttpStatus.CONFLICT).body(result); // 409 Conflict | ||
|
||
case "You have successfully subscribed!": | ||
return ResponseEntity.status(HttpStatus.CREATED).body(result); // 201 Created | ||
|
||
case "You have successfully re-subscribed!": | ||
return ResponseEntity.status(HttpStatus.OK).body(result); // 200 OK | ||
|
||
default: | ||
return ResponseEntity.status(HttpStatus.OK).body(result); // Default 200 OK | ||
} | ||
} catch (Exception e) { | ||
// Handle unexpected errors | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) | ||
.body("An error occurred while processing your subscription."); | ||
} | ||
} | ||
|
||
// Unsubscribe Endpoint | ||
@GetMapping("/unsubscribe") | ||
public String unsubscribe(@RequestParam String token) { | ||
return newsletterService.unsubscribe(token); | ||
public ResponseEntity<String> unsubscribe(@RequestParam String token) { | ||
try { | ||
String result = newsletterService.unsubscribe(token); | ||
|
||
switch (result) { | ||
case "Invalid or expired token.": | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(result); // 404 Not Found | ||
|
||
case "You are already unsubscribed.": | ||
return ResponseEntity.status(HttpStatus.CONFLICT).body(result); // 409 Conflict | ||
|
||
case "You have successfully unsubscribed!": | ||
return ResponseEntity.status(HttpStatus.OK).body(result); // 200 OK | ||
|
||
default: | ||
return ResponseEntity.status(HttpStatus.OK).body(result); // Default 200 OK | ||
} | ||
} catch (Exception e) { | ||
// Handle unexpected errors | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) | ||
.body("An error occurred while processing your unsubscription."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,4 +86,4 @@ public CorsFilter corsFilter() { | |
return new CorsFilter(corsConfigurationSource()); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters