-
Notifications
You must be signed in to change notification settings - Fork 1
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 #38 from RitehWebTeam/matching
Add matching functionality.
- Loading branch information
Showing
32 changed files
with
816 additions
and
157 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
94 changes: 94 additions & 0 deletions
94
backend/src/main/java/com/rimatch/rimatchbackend/controller/MatchController.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,94 @@ | ||
package com.rimatch.rimatchbackend.controller; | ||
|
||
import com.infobip.ApiException; | ||
import com.rimatch.rimatchbackend.dto.DisplayUserDto; | ||
import com.rimatch.rimatchbackend.dto.MatchDto; | ||
import com.rimatch.rimatchbackend.lib.InfobipClient; | ||
import com.rimatch.rimatchbackend.model.Match; | ||
import com.rimatch.rimatchbackend.model.User; | ||
import com.rimatch.rimatchbackend.service.MatchService; | ||
import com.rimatch.rimatchbackend.service.UserService; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.validation.Valid; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@RestController | ||
@RequestMapping("api/match") | ||
public class MatchController { | ||
|
||
@Autowired | ||
MatchService matchService; | ||
|
||
@Autowired | ||
UserService userService; | ||
|
||
/*@Autowired | ||
private SendEmailLib sendEmailLib;*/ | ||
|
||
@Autowired | ||
private InfobipClient infobipClient; | ||
|
||
@GetMapping("/potential") | ||
public List<DisplayUserDto> getPotentinalMatch(HttpServletRequest request){ | ||
String authToken = request.getHeader("Authorization"); | ||
User user = userService.getUserByToken(authToken); | ||
List<DisplayUserDto> list = matchService.findPotentialMatches(user); | ||
return list; | ||
} | ||
|
||
@PostMapping("/accept") | ||
public ResponseEntity<Match> accept(HttpServletRequest request, @Valid @RequestBody MatchDto matchDto) throws ApiException { | ||
String authToken = request.getHeader("Authorization"); | ||
User user = userService.getUserByToken(authToken); | ||
userService.insertToSeenUserIds(user,matchDto.getUserId()); | ||
|
||
Optional<User> matchedUser = userService.getUserById(matchDto.getUserId()); | ||
|
||
Match match = matchService.findMatch(user.getId(),matchedUser.get().getId()); | ||
|
||
if(match != null){ | ||
|
||
var recepients = new ArrayList<>(List.of("dominikkovacevic6@gmail.com")); | ||
var string = String.format("Hello %s you just matched with %s %s!",matchedUser.get().getFirstName(),user.getFirstName(),user.getLastName()); | ||
infobipClient.sendEmail(recepients,"You got a match!", string); | ||
infobipClient.sendSms(string); | ||
return ResponseEntity.ok(matchService.finishMatch(match,true)); | ||
} | ||
|
||
return ResponseEntity.ok(matchService.saveMatch(user.getId(),matchDto.getUserId())); | ||
} | ||
|
||
@PostMapping("/reject") | ||
public ResponseEntity<?> reject(HttpServletRequest request, @Valid @RequestBody MatchDto matchDto){ | ||
String authToken = request.getHeader("Authorization"); | ||
User user = userService.getUserByToken(authToken); | ||
|
||
userService.insertToSeenUserIds(user,matchDto.getUserId()); | ||
Optional<User> matchedUser = userService.getUserById(matchDto.getUserId()); | ||
matchedUser.ifPresent(value -> userService.insertToSeenUserIds(value, user.getId())); | ||
|
||
Match match = matchService.findMatch(user.getId(),matchedUser.get().getId()); | ||
|
||
if(match != null){ | ||
return ResponseEntity.ok(matchService.finishMatch(match,false)); | ||
} | ||
|
||
return ResponseEntity.ok("Test"); | ||
} | ||
|
||
// all matches for user sending the reuqest | ||
@GetMapping("/all") | ||
public ResponseEntity<List<DisplayUserDto>> getAllMatches(HttpServletRequest request){ | ||
String authToken = request.getHeader("Authorization"); | ||
User user = userService.getUserByToken(authToken); | ||
List<DisplayUserDto> list = matchService.getAllSuccessfulMatchedUsers(user); | ||
return ResponseEntity.ok(list); | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
backend/src/main/java/com/rimatch/rimatchbackend/dto/DisplayUserDto.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,36 @@ | ||
package com.rimatch.rimatchbackend.dto; | ||
|
||
import com.rimatch.rimatchbackend.model.User; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class DisplayUserDto { | ||
|
||
private String id; | ||
|
||
private String firstName; | ||
|
||
private String lastName; | ||
|
||
private String description; | ||
|
||
private String profileImageUrl; | ||
|
||
private String location; | ||
|
||
private Character gender; | ||
|
||
private int age; | ||
|
||
public void initDisplayUser(User user){ | ||
this.id = user.getId(); | ||
this.firstName = user.getFirstName(); | ||
this.lastName = user.getLastName(); | ||
this.description = user.getDescription(); | ||
this.profileImageUrl = user.getProfileImageUrl(); | ||
this.location = user.getLocation(); | ||
this.gender = user.getGender(); | ||
this.age = user.getAge(); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
backend/src/main/java/com/rimatch/rimatchbackend/dto/MatchDto.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,15 @@ | ||
package com.rimatch.rimatchbackend.dto; | ||
|
||
import jakarta.validation.Valid; | ||
import lombok.Getter; | ||
import lombok.NonNull; | ||
|
||
@Getter | ||
public class MatchDto { | ||
|
||
@Valid | ||
|
||
@NonNull | ||
private String userId; | ||
|
||
} |
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
94 changes: 94 additions & 0 deletions
94
backend/src/main/java/com/rimatch/rimatchbackend/lib/InfobipClient.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,94 @@ | ||
package com.rimatch.rimatchbackend.lib; | ||
|
||
import com.infobip.*; | ||
import com.infobip.api.EmailApi; | ||
import com.infobip.api.SmsApi; | ||
import com.infobip.model.SmsAdvancedTextualRequest; | ||
import com.infobip.model.SmsDestination; | ||
import com.infobip.model.SmsResponse; | ||
import com.infobip.model.SmsTextualMessage; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Component | ||
public class InfobipClient { | ||
|
||
@Value("${infobip.api-key}") | ||
private String API_KEY; | ||
|
||
@Value("${infobip.base-url}") | ||
private String BASE_URL; | ||
|
||
// Sender email address must be verified on the infobip portal for your account | ||
@Value("${infobip.sender-email}") | ||
private String SENDER_EMAIL_ADDRESS; | ||
|
||
@Value("${infobip.phone-number}") | ||
private String PHONE_NUMBER; | ||
|
||
private String getSenderEmail() { | ||
return String.format("RiMatchApp <%s>", SENDER_EMAIL_ADDRESS); | ||
} | ||
|
||
private EmailApi initEmailApi() { | ||
return new EmailApi(initApiClient()); | ||
} | ||
|
||
private SmsApi initSmsApi(){ | ||
return new SmsApi(initApiClient()); | ||
} | ||
|
||
private ApiClient initApiClient(){ | ||
return ApiClient.forApiKey(ApiKey.from(API_KEY)).withBaseUrl(BaseUrl.from(BASE_URL)).build(); | ||
} | ||
|
||
public void sendSms(String text){ | ||
|
||
var smsApi = initSmsApi(); | ||
|
||
SmsTextualMessage smsMessage = new SmsTextualMessage() | ||
.from("InfoSMS") | ||
.addDestinationsItem(new SmsDestination().to(PHONE_NUMBER)) | ||
.text(text); | ||
|
||
SmsAdvancedTextualRequest smsMessageRequest = new SmsAdvancedTextualRequest() | ||
.messages(List.of(smsMessage)); | ||
|
||
smsApi.sendSmsMessage(smsMessageRequest) | ||
.executeAsync(new ApiCallback<SmsResponse>() { | ||
@Override | ||
public void onSuccess(SmsResponse result, int responseStatusCode, Map<String, List<String>> responseHeaders) { | ||
|
||
} | ||
@Override | ||
public void onFailure(ApiException exception, int responseStatusCode, Map<String, List<String>> responseHeaders) { | ||
} | ||
}); | ||
} | ||
|
||
public void sendEmail(List<String> recepientEmailAddress, String subject, String text) throws ApiException { | ||
var sendEmailApi = initEmailApi(); | ||
|
||
|
||
|
||
try { | ||
var emailResponse = sendEmailApi.sendEmail(recepientEmailAddress) | ||
.from(getSenderEmail()) | ||
.subject(subject) | ||
.text(text) | ||
.execute(); | ||
|
||
System.out.println("Response body: " + emailResponse); | ||
|
||
var reportsResponse = sendEmailApi.getEmailDeliveryReports().execute(); | ||
System.out.println(reportsResponse.getResults()); | ||
} catch (ApiException e) { | ||
System.out.println("HTTP status code: " + e.responseStatusCode()); | ||
System.out.println("Response body: " + e.rawResponseBody()); | ||
throw e; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
backend/src/main/java/com/rimatch/rimatchbackend/model/Match.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,27 @@ | ||
package com.rimatch.rimatchbackend.model; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.Data; | ||
import lombok.NonNull; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
@Document(collection = "matches") | ||
@Data | ||
public class Match { | ||
|
||
@Id | ||
private String id; | ||
|
||
@NonNull | ||
@NotBlank | ||
private String firstUserId; | ||
|
||
@NonNull | ||
@NotBlank | ||
private String secondUserId; | ||
|
||
private boolean accepted = false; | ||
|
||
private boolean finished = false; | ||
} |
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 |
---|---|---|
|
@@ -2,8 +2,6 @@ | |
import lombok.*; | ||
|
||
@Data | ||
@Getter | ||
@Setter | ||
public class Preferences { | ||
|
||
private int ageGroupMin; | ||
|
Oops, something went wrong.