forked from ajaynegi45/LibraryMan-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ajaynegi45#89 from shreya5653/mybranch-shreya5653
Implement Backend Functionality for Email Sending
- Loading branch information
Showing
13 changed files
with
166 additions
and
144 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
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
75 changes: 51 additions & 24 deletions
75
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,44 +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.*; | ||
|
||
import java.util.Map; | ||
|
||
@RestController | ||
@RequestMapping("/api/newsletter") | ||
public class NewsletterController { | ||
|
||
@Autowired | ||
private NewsletterService newsletterService; | ||
private final NewsletterService newsletterService; | ||
|
||
public NewsletterController(NewsletterService newsletterService) { | ||
this.newsletterService = newsletterService; | ||
} | ||
|
||
// Subscribe endpoint | ||
// Subscribe Endpoint | ||
@PostMapping("/subscribe") | ||
public ResponseEntity<String> subscribe(@RequestBody Map<String, String> requestBody) { | ||
String email = requestBody.get("email"); | ||
public ResponseEntity<String> subscribe(@RequestParam String email) { | ||
try { | ||
String result = newsletterService.subscribe(email); | ||
|
||
// Call the service to handle subscription | ||
String response = newsletterService.subscribe(email); | ||
switch (result) { | ||
case "Invalid email format.": | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result); // 400 Bad Request | ||
|
||
// Return response from the service | ||
if (response.equals("Invalid email format.") || response.equals("Email is already subscribed.")) { | ||
return ResponseEntity.badRequest().body(response); | ||
} | ||
case "Email is already subscribed.": | ||
return ResponseEntity.status(HttpStatus.CONFLICT).body(result); // 409 Conflict | ||
|
||
return ResponseEntity.ok(response); | ||
} | ||
case "You have successfully subscribed!": | ||
return ResponseEntity.status(HttpStatus.CREATED).body(result); // 201 Created | ||
|
||
// Unsubscribe endpoint using token | ||
@GetMapping("/unsubscribe/{token}") | ||
public ResponseEntity<String> unsubscribe(@PathVariable String token) { | ||
String response = newsletterService.unsubscribe(token); | ||
case "You have successfully re-subscribed!": | ||
return ResponseEntity.status(HttpStatus.OK).body(result); // 200 OK | ||
|
||
// Check if the response indicates an error | ||
if (response.equals("Invalid or expired token.") || response.equals("You are already unsubscribed.")) { | ||
return ResponseEntity.badRequest().body(response); | ||
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."); | ||
} | ||
} | ||
|
||
return ResponseEntity.ok(response); | ||
// Unsubscribe Endpoint | ||
@GetMapping("/unsubscribe") | ||
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."); | ||
} | ||
} | ||
} |
Oops, something went wrong.