Skip to content
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

Bf 17 account page #65

Merged
merged 31 commits into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ae723fc
BF-84 Initial Profile page design
AbdelrahmanMosly Dec 27, 2022
fd93ce4
BF-85 add desired classes
AbdelrahmanMosly Dec 28, 2022
201ee17
BF-86 create requests
AbdelrahmanMosly Dec 28, 2022
c73c8aa
BF-86 fix return of the request in front
AbdelrahmanMosly Dec 28, 2022
e9e7d37
BF-87 display response
AbdelrahmanMosly Dec 28, 2022
1b985ff
Merge branch 'Milestone3' into BF-17-Account-page
AbdelrahmanMosly Dec 28, 2022
dcecf46
BF-88 get data from DB
AbdelrahmanMosly Dec 28, 2022
4c51e11
BF-86 use real data
AbdelrahmanMosly Dec 28, 2022
d1c359c
BF-89 add icons for profile page
AbdelrahmanMosly Dec 28, 2022
dfe4776
BF-86 edit request mapping
AbdelrahmanMosly Dec 28, 2022
b5fa26c
BF-86 edit request mapping
AbdelrahmanMosly Dec 28, 2022
1077d6c
BF-86 add request to security
AbdelrahmanMosly Dec 28, 2022
a9e1482
BF-86 edit request mapping
AbdelrahmanMosly Dec 28, 2022
2908b66
BF-90 deisgn with real data and tested
AbdelrahmanMosly Dec 28, 2022
3414e96
import dart::ffi
AbdelrahmanMosly Dec 28, 2022
1747372
Revert "BF-90 deisgn with real data and tested"
AbdelrahmanMosly Dec 28, 2022
2b07ce5
Revert "import dart::ffi"
AbdelrahmanMosly Dec 28, 2022
063128c
Revert "Revert "BF-90 deisgn with real data and tested""
AbdelrahmanMosly Dec 28, 2022
b2b936a
BF-17 remove import dart::ffi from model in front
AbdelrahmanMosly Dec 28, 2022
75b861b
BF-17 change package name
AbdelrahmanMosly Dec 29, 2022
22b1545
BF-17 import package
AbdelrahmanMosly Dec 29, 2022
039d02a
BF-17 deploy back to update front
AbdelrahmanMosly Dec 29, 2022
e6d5746
BF-91 edit custom field
AbdelrahmanMosly Dec 29, 2022
eb1af8e
BF-92 final touches
AbdelrahmanMosly Dec 29, 2022
ed5cc47
Merge branch 'Milestone3' into BF-17-Account-page
AbdelrahmanMosly Dec 29, 2022
40e8781
removing warning and redundant import due to merge
AbdelrahmanMosly Dec 29, 2022
0b54e2c
BF-65 remove duplicate package
AbdelrahmanMosly Dec 30, 2022
5d50cd2
BF-65remove duplicate tests
AbdelrahmanMosly Dec 30, 2022
5364b4e
BF-17 tests
AbdelrahmanMosly Dec 30, 2022
198ee9a
BF-17 show email
AbdelrahmanMosly Dec 30, 2022
f8cf9b4
BF-17 remove unnecessary debug print
AbdelrahmanMosly Dec 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions backend/src/main/java/com/brainfood/backend/DAO.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package com.brainfood.backend;

import com.brainfood.backend.db_entities.IngredientDB;
import com.brainfood.backend.db_entities.RecipeDB;
import com.brainfood.backend.db_repositories.IngredientRepository;
import com.brainfood.backend.db_repositories.RecipeRepository;
import com.brainfood.backend.models.Recipe;
import com.brainfood.backend.models.ShortRecipe;
import com.brainfood.backend.models.UserProfile;
import com.brainfood.security.repository.UserCredentialsRepository;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import com.brainfood.backend.db_entities.IngredientDB;
import com.brainfood.backend.db_entities.RecipeDB;
import com.brainfood.backend.db_entities.User;
import com.brainfood.backend.db_entities.UserFavRecipes;
import com.brainfood.backend.db_entities.UserFavRecipesCK;
import com.brainfood.backend.db_repositories.IngredientRepository;
import com.brainfood.backend.db_repositories.RecipeRepository;
import com.brainfood.backend.db_repositories.UserFavRecipesRepository;
import com.brainfood.backend.db_repositories.UserRepository;
import com.brainfood.backend.models.Recipe;
import com.brainfood.backend.models.ShortRecipe;
import com.brainfood.security.Response;

@Component
Expand All @@ -30,9 +32,10 @@ public class DAO {

@Autowired
IngredientRepository ingredientRepository;

@Autowired
UserRepository userRepository;
@Autowired
UserCredentialsRepository userCredentialsRepository;

@Autowired
UserFavRecipesRepository userFavRecipesRepository;
Expand Down Expand Up @@ -99,6 +102,18 @@ public List<String> getAllIngredients() {
return ingredientRepository.getDistinctByName();
}


public UserProfile getUserProfile(){
String username= SecurityContextHolder.getContext().getAuthentication().getName();
User user = userRepository.findByUsername(username);
String email= userCredentialsRepository.findByUsername(username).getEmail();

return UserProfile.builder().username(username)
.birthdate(user.getBirthdate())
.height(user.getHeight())
.weight(user.getWeight())
.email(email).build();
}
public Response addFavRecipeByUsername(String username, String recipeID) {
User user = userRepository.findByUsername(username);
String userID = user.getID();
Expand All @@ -111,8 +126,7 @@ public Response addFavRecipeByUsername(String username, String recipeID) {
public List<RecipeDB> getFavRecipesByUsername(String username) {
User user = userRepository.findByUsername(username);
String userID = user.getID();
List<RecipeDB> recipesFav = userRepository.findFavRecipesById(userID);
return recipesFav;
return userRepository.findFavRecipesById(userID);
}

public Response removeFavRecipeByUsername(String username, String recipeID) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.brainfood.backend.controllers;

import com.brainfood.backend.DAO;
import com.brainfood.backend.models.UserProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@ComponentScan
@RestController
@RequestMapping("account_settings")
public class UserProfileController {

@Autowired
Copy link
Member

@Deffo0 Deffo0 Dec 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Autowired here isn't necessary, as you can replace it by constructor injection which has alot of advantages as following:

  • the dependencies are clearly identified. There is no way to forget one when testing, or instantiating the object in any other circumstance (like creating the bean instance explicitly in a config class).

  • the dependencies can be final, which helps with robustness and thread-safety.

  • you don't need reflection to set the dependencies. InjectMocks is still usable, but not necessary. you can just create mocks by yourself and inject them by simply calling the constructor.

  • by the autowired approach, you are allowing anyone (in different class outside/inside the Spring container) to create an instance using default constructor (like new SomeService()), which is NOT good as you need SomeOtherService object (as a dependency) for your SomeService.

You may have some questions like "is this is applicable in the stereo type component as you don't call it?"

The answer is yes and you can do it even for JPA repositories.

how does spring inject the dependency in this case ?

Spring scans your classes for constructor that matches your class' fields. Find details here:
Additional Details

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't prefer Autowired my self as well here but I decided to use it to be consistent with the rest of the project.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good, so we should mark it as an issue. I think it will need a huge refactoring, but it will be worthy.

DAO DAO;
@GetMapping("/userProfile")
public UserProfile getUserProfile(){
return DAO.getUserProfile();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.brainfood.backend.models;

import lombok.*;

import java.util.Date;

@Getter @Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class UserProfile {

private String username;

private String email;

private float height;

private float weight;

private Date birthdate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import org.springframework.stereotype.Service;

import com.brainfood.security.model.PasswordResetToken;
import com.brainfood.security.model.UserCredentials;
import com.brainfood.security.repository.PasswordResetTokenRepository;
import com.brainfood.security.model.UserCredentials;

@Service
public class PasswordResetManager {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.authorizeHttpRequests().requestMatchers("/home/**").authenticated().and()
.authorizeHttpRequests().requestMatchers(HttpMethod.GET, "/share/recipe/**").permitAll().and()
.authorizeHttpRequests().requestMatchers("/search/**").authenticated().and()
.authorizeHttpRequests().requestMatchers("/account_settings/**").authenticated().and()
.authorizeHttpRequests().requestMatchers(HttpMethod.POST, "/signup/**").permitAll().and()
.formLogin().loginPage("/signin").defaultSuccessUrl("/home").loginProcessingUrl("/signin").and()
.logout().logoutUrl("/signout").logoutRequestMatcher(new AntPathRequestMatcher("/signout"))
Expand Down
78 changes: 0 additions & 78 deletions backend/src/test/java/com/brainfood/backend/BodyInfoTest.java

This file was deleted.

137 changes: 0 additions & 137 deletions backend/src/test/java/com/brainfood/backend/IngredientTest.java

This file was deleted.

Loading