-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FEAT:newsletter unsubscribe endpoint #699
Open
odero-lavenda
wants to merge
1
commit into
hngprojects:dev
Choose a base branch
from
odero-lavenda:gaga
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 18 additions & 4 deletions
22
src/main/java/hng_java_boilerplate/newsletter/controller/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,40 @@ | ||
package hng_java_boilerplate.newsletter.controller; | ||
|
||
import hng_java_boilerplate.exception.UnAuthorizedException; | ||
import hng_java_boilerplate.newsletter.dto.SubscribeRequest; | ||
import hng_java_boilerplate.newsletter.dto.SubscribeResponse; | ||
import hng_java_boilerplate.newsletter.dto.UnsubscribeResponse; | ||
import hng_java_boilerplate.newsletter.service.NewsletterService; | ||
import hng_java_boilerplate.user.entity.User; | ||
import hng_java_boilerplate.user.service.UserService; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v1/newsletter-subscription") | ||
public class NewsletterController { | ||
private final NewsletterService newsletterService; | ||
private final UserService userService; | ||
|
||
@PostMapping | ||
public ResponseEntity<SubscribeResponse> subscribe(@RequestBody @Valid SubscribeRequest request) { | ||
return ResponseEntity.status(HttpStatus.CREATED) | ||
.body(newsletterService.subscribeToNewsletter(request)); | ||
} | ||
// ✅ Unsubscribe from Newsletter | ||
@DeleteMapping("/{subscriberId}") | ||
public ResponseEntity<?> unsubscribe( | ||
@PathVariable("subscriberId") String subscriberId | ||
) { | ||
User authenticateduser =userService.getLoggedInUser(); | ||
if(authenticateduser==null){ | ||
throw new UnAuthorizedException("unathorised user"); | ||
} | ||
UnsubscribeResponse bun=newsletterService.unsubscribeFromNews(subscriberId); | ||
return ResponseEntity.status(HttpStatus.OK).body(bun); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/hng_java_boilerplate/newsletter/dto/UnsubscribeRequest.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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package hng_java_boilerplate.newsletter.dto; | ||
|
||
import jakarta.validation.constraints.Email; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record UnsubscribeRequest( | ||
@Email(message="email must be a valid email") | ||
@NotBlank(message = "email cannot be blank") | ||
String email | ||
) | ||
{ | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/hng_java_boilerplate/newsletter/dto/UnsubscribeResponse.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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package hng_java_boilerplate.newsletter.dto; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public record UnsubscribeResponse(int status_code,String message) { | ||
} |
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
89 changes: 89 additions & 0 deletions
89
src/test/java/hng_java_boilerplate/newsletter/NewsletterServiceTest.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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package hng_java_boilerplate.newsletter; | ||
|
||
|
||
import hng_java_boilerplate.exception.NotFoundException; | ||
import hng_java_boilerplate.exception.UnAuthorizedException; | ||
import hng_java_boilerplate.newsletter.dto.UnsubscribeResponse; | ||
import hng_java_boilerplate.newsletter.entity.Newsletter; | ||
import hng_java_boilerplate.newsletter.repository.NewsletterRepository; | ||
import hng_java_boilerplate.newsletter.service.NewsletterService; | ||
import hng_java_boilerplate.user.entity.User; | ||
import hng_java_boilerplate.user.repository.UserRepository; | ||
import hng_java_boilerplate.user.service.UserService; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class NewsletterServiceTest { | ||
|
||
@Mock | ||
private UserRepository userRepository; | ||
|
||
@Mock | ||
private NewsletterRepository newsletterRepository; | ||
|
||
@InjectMocks | ||
private NewsletterService newsletterService; | ||
|
||
@Test | ||
public void testUnsubscribeFromNews_successfulUnsubscribe() { | ||
String subscriberId = "testId"; | ||
User user = new User(); | ||
user.setId(subscriberId); | ||
Newsletter subscriber = new Newsletter(); | ||
subscriber.setId(subscriberId); | ||
|
||
when(userRepository.findById(subscriberId)).thenReturn(Optional.of(user)); | ||
when(newsletterRepository.findById(subscriberId)).thenReturn(Optional.of(subscriber)); | ||
|
||
UnsubscribeResponse response = newsletterService.unsubscribeFromNews(subscriberId); | ||
|
||
assertNull(response); // Or assert the expected response if not null | ||
verify(newsletterRepository, times(1)).delete(subscriber); | ||
} | ||
|
||
@Test | ||
public void testUnsubscribeFromNews_userNotFound() { | ||
String subscriberId = "nonExistentId"; | ||
when(userRepository.findById(subscriberId)).thenReturn(Optional.empty()); | ||
|
||
assertThrows(NotFoundException.class, () -> newsletterService.unsubscribeFromNews(subscriberId)); | ||
verify(newsletterRepository, never()).delete(any()); | ||
} | ||
|
||
@Test | ||
public void testUnsubscribeFromNews_subscriberNotFound() { | ||
String subscriberId = "testId"; | ||
User user = new User(); | ||
user.setId(subscriberId); | ||
|
||
when(userRepository.findById(subscriberId)).thenReturn(Optional.of(user)); | ||
when(newsletterRepository.findById(subscriberId)).thenReturn(Optional.empty()); | ||
|
||
assertThrows(NotFoundException.class, () -> newsletterService.unsubscribeFromNews(subscriberId)); | ||
verify(newsletterRepository, never()).delete(any()); | ||
} | ||
|
||
// @Test | ||
// public void testUnsubscribeFromNews_unauthorizedUser() { | ||
// String subscriberId = "testId"; | ||
// User user = new User(); | ||
// user.setId("differentId"); | ||
// Newsletter subscriber = new Newsletter(); | ||
// subscriber.setId(subscriberId); | ||
|
||
// when(userRepository.findById(subscriberId)).thenReturn(Optional.of(user)); | ||
// when(newsletterRepository.findById(subscriberId)).thenReturn(Optional.of(subscriber)); | ||
// | ||
// assertThrows(UnAuthorizedException.class, () -> newsletterService.unsubscribeFromNews(subscriberId)); | ||
// verify(newsletterRepository, never()).delete(any()); | ||
// } | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this commented out section