Saving a element from one document to user document - java

I have those two entites:
#Document
public class User {
#Id
private String id;
#Indexed(unique = true)
#NonNull
private String username;
#Indexed(unique = true)
#NonNull
private String email;
#JsonIgnore
#NonNull
private String password;
#Document("adventureholidays")
public class AdventureHolidays {
#Id
private String id;
private String title;
private String description;
private String state;
private String typeOfAdventureHolidays;
private String image;
I want to create a service that will save into user document element from adventureholidays.
I have a service to return a random element from adventureholidays document, so If user want to save that document I would like to save title of that document into user document.
So somehow I need to take a ID of current adventureholiday that is provided and then to save to current logged user.

I am just giving you the logic, add
#DBRef
private AdventureHolidays adventureHolidays;
in your User class it will map both classes, then get the id of holiday from the save button using link or value from your form and then using that id in find() function and fetch the data of that particular holiday store it in a object ie. holiday.
Now just save it in service class inside a method like this:
User user=new User();
user.setAdventureHolidays(holiday);
// create a userRepository which extends MongoRepository
userRepository.save(user);

Related

dto entity for form or create special dto form entity for form

Lets imagine that I have user entity and some registration form. Should I return userDto or I should return some RegistrationForm entity and map it to domain model.
Normally you don't need a RegistrationForm entity and User entity would be enough to keep user data. On the other hand, you can create two separate dto e.g. SignupRequest for sending form data to the corresponding service and SignupResponse for sending response after registering the user.
Here is an example usage:
#Data
public class SignupRequest {
private String firstName;
private String lastName;
private String username;
private String password;
}
#Data
public class SignupResponse {
String message;
Long userId;
String username;
String accessToken;
String refreshToken;
}

Hide sensitive data in an association relationship in java #JsonProperty

How can I hide some sensitive data on this example. I'm testing APIs in rest client (Postman), when I call Api List of Bills, I want to hide some data. In BillsDto I want to hide username, password and user age fields. Is it possible to do this in my BillsDto class (not in UserDto). I know I can hide some fields using #JsonProperty but how to do it for some fields belonging to another class?
***BillsDto***
public class BillsDto {
private String numberBills;
private double amount;
private Date deadlinePayment
private UserDto user; // try to hide username, password, age from BillsDto
}
***UserDto***
public class UserDto {
private String number_id;
private String username;
private String password;
private String firstName;
private String lastName;
private String age;
}
I know I can hide some fields using #JsonProperty but how to do it for some fields belonging to another class?
The fact that you're using UserDto as a nested object somewhere, doesn't change the serialization policy that you can express through data binding annotations in the UserDto.
If you can change UserDto, apply #JsonProperty with it's property access set to JsonProperty.Access.WRITE_ONLY on the fields want to hide during serialization.
public class UserDto {
private String number_id;
#JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String username;
#JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;
private String firstName;
private String lastName;
#JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String age;
}
If for some reason, you want to achieve this by editing BillsDto only, then you can implement a custom serializer for UserDto and apply it by making use of the #JsonSerialize. But to ensure that you're not disclosing the sensitive data somewhere, it would be better to apply this policy in one place - in the UserDto, because you or one of your colleagues might simply forget to #JsonSerialize in some of the classes which uses UserDto.

I'm stuck trying to save #DBRef inside document

I have document user and document adventureHolidays. There is a lot of users and as well adventure holidays. So far I created a controller that return me a random element from adventureHolidays document. On that page I have button to randomize again and on each click new document from adventureHoliday is provided. What is my goal right now? I want to create a button that will actually save a current document that is provided as field in user document, so he can see on his profile which adventureHolidays he has saved.
This is user entity
#Document
public class User {
#Id
private String id;
#Indexed(unique = true)
#NonNull
private String username;
#Indexed(unique = true)
#NonNull
private String email;
#JsonIgnore
#NonNull
private String password;
#DBRef
private List<AdventureHolidays> adventureHolidaysList;
And this is AdventureHoldiays
#Document("adventureholidays")
public class AdventureHolidays {
#Id
private String id;
private String title;
private String description;
private String state;
private String typeOfAdventureHolidays;
private String image;
How I can now save document that is random returned to user to user entity? I even dont know where to start. Guess I need ID and that to save somehow it to field in user document

How to update only chosen records from form?

The main question is, how to update just a few chosen fields from our form. I would like to give user choice which fields they want to update. For example, I have the form class:
public class UserRegistrationform {
private Integer userId;
#NotNull
private String name;
#NotNull
private String surname;
#Email
#NotNull
private String email;
#NotNull
private Integer genderId;
#NotNull
private Integer groupId;
#NotNull
private List<ContactInfoDto> contactsInfo;
#NotNull
private String userSecretkey;
#NotNull
private String password;
#NotNull
private boolean enabled;
#NotNull
private boolean resetPassword;
After that I'm setting fields in #Entity class User, and for example, if user want to change just their name and surname, I want to take the rest of the fields from the existing User, by findById() method and after that change a few fields and save the changed object to the database.
#BeshambherChaukhwan ok, maybe will be easier where I will use example, so I have a controller when is method:
#PostMapping(value = "{id}/updateuser")
public ResponseDTO updateUser(#PathVariable int id, #RequestBody UserRegistrationform userForm, BindingResult bindingResult) {
User user = userRepository.findById(id);
if (user == null) {
return ResponseDTO.of(Messages.Error_UserNotFound_MSG.getMessage(), ErrorCodes.NOT_FOUND);
} else if (bindingResult.hasErrors()) {
return ResponseDTO.of(bindingResult.getAllErrors().toString(), ErrorCodes.INVALID_FIELDS);
}
user.setName(userForm.getName);
user.setSurname(userForm.getSurname);
user.setEmail(userForm.getEmail);
.
.
.
userRepository.save(user);
return ResponseDTO.of(Messages.SUCCESS_Update_MSG.getMessage());
}
The problem is, like you noticed when in form someone miss few fields, and I will update right fields by null values

returning specific version of DTO based on user role

I have a JobDTO class
class JobDTO{
private Integer id;
private String jobTitle;
private String secreateData;
...
}
I have a BidDTO class
class BidDTO{
private Integer id;
private String bidDetails;
private JobDTO jobDTO;
public BidDTO(Integer id, String bidDetails, JobDTO jobDTO){
this.id = id;
this.bidDetails = bidDetails;
this.jobDTO = jobDTO;
}
}
The reason I have JobDTO in BidDTO is because when I return a bid I need to return the related job details as well. The question is that what I want to hide the secretData in JobDTO from user based on users's role?
One solution could be to put individual JobDTO fields in BidDTO that I want to show to user rather than having a JobDTO object as part of it but what if there are 100 fields in JobDTO and I only have one secretData field that want to hide.

Categories