Skip to content
Snippets Groups Projects
Commit c04a9397 authored by abdu's avatar abdu
Browse files

add toasts (FE), handle case user already exists (BE)

parent 0358e5ea
No related branches found
No related tags found
No related merge requests found
...@@ -23,4 +23,8 @@ export default { ...@@ -23,4 +23,8 @@ export default {
text-align: center; text-align: center;
margin-top: 60px; margin-top: 60px;
} }
.toast:not(.show) {
display: block !important;
}
</style> </style>
...@@ -9,8 +9,7 @@ export default { ...@@ -9,8 +9,7 @@ export default {
userName: '', userName: '',
password: '' password: ''
}, },
repeatedPassword: '', repeatedPassword: ''
testname: ''
} }
}, },
computed: { computed: {
...@@ -23,20 +22,27 @@ export default { ...@@ -23,20 +22,27 @@ export default {
async signUp() { async signUp() {
let url = "http://localhost:9876/user/signup" let url = "http://localhost:9876/user/signup"
this.$bvModal.hide('signup-modal');
console.log(this.user)
const response = await fetch(url, { const response = await fetch(url, {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json" "Content-Type": "application/json"
}, },
body: JSON.stringify(this.user) body: JSON.stringify({
"userName": this.user.userName,
"password": this.user.password
})
}); });
console.log(response) if(response.status === 201){
this.$bvToast.show('account-created-toast')
} else if(response.status === 409){
this.$bvToast.show('account-not-created-toast')
}
}, },
async signIn(){
}
} }
} }
</script> </script>
...@@ -63,9 +69,9 @@ export default { ...@@ -63,9 +69,9 @@ export default {
</b-modal> </b-modal>
<!-- sign-in modal --> <!-- sign-in modal -->
<b-modal hide-header id="signin-modal"> <b-modal :ok-disabled="checkEmptyFields" @ok="signIn" hide-header id="signin-modal">
<b-input class="mb-4 mt-4" id="username" type="text" placeholder="enter username"></b-input> <b-input v-model="user.userName" class="mb-4 mt-4" id="username" type="text" placeholder="enter username"></b-input>
<b-input class="mb-4" id="password" type="password" placeholder="enter password"></b-input> <b-input v-model="user.password" class="mb-4" id="password" type="password" placeholder="enter password"></b-input>
</b-modal> </b-modal>
</b-card-body> </b-card-body>
<b-card-footer> <b-card-footer>
...@@ -76,8 +82,14 @@ export default { ...@@ -76,8 +82,14 @@ export default {
<b-col></b-col> <b-col></b-col>
</b-row> </b-row>
<!-- <!--
----- toasts & alerts -----
--> -->
<b-toast no-close-button auto-hide-delay="5000" variant="success" id="account-created-toast" title="success">
account for {{ this.user.userName }} has been created
</b-toast>
<b-toast no-close-button auto-hide-delay="5000" variant="danger" id="account-not-created-toast" title="error">
user {{ this.user.userName }} already exists
</b-toast>
</b-container> </b-container>
</template> </template>
......
...@@ -18,11 +18,12 @@ public class UserController { ...@@ -18,11 +18,12 @@ public class UserController {
@PostMapping("/signup") @PostMapping("/signup")
public ResponseEntity<UserDTO> singUp(@RequestBody UserDTO userDTO) { public ResponseEntity<UserDTO> singUp(@RequestBody UserDTO userDTO) {
UserDTO newUser = userService.createUser(userDTO); UserDTO newUser = userService.createUser(userDTO);
if(newUser != null) { if(newUser != null) {
return new ResponseEntity<>(newUser, HttpStatus.CREATED); return new ResponseEntity<>(newUser, HttpStatus.CREATED);
} }
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); return new ResponseEntity<>(HttpStatus.CONFLICT);
} }
@PostMapping("/login") @PostMapping("/login")
......
...@@ -24,14 +24,12 @@ public class UserService { ...@@ -24,14 +24,12 @@ public class UserService {
public UserDTO createUser(UserDTO userDTO) { public UserDTO createUser(UserDTO userDTO) {
User newUser; User newUser;
if(userRepository.existsByUserId(userDTO.getUserId()) || userRepository.existsByUserName(userDTO.getUserName())){ if(!(userRepository.existsByUserId(userDTO.getUserId()) || userRepository.existsByUserName(userDTO.getUserName()))){
newUser = userRepository.findByUserName(userDTO.getUserName());
} else {
newUser = userMapper.dtoToEntity(userDTO); newUser = userMapper.dtoToEntity(userDTO);
return userMapper.entityToDto(userRepository.save(newUser));
} }
User createdUser = userRepository.save(newUser); return null;
return userMapper.entityToDto(createdUser);
} }
public boolean login(HttpSession session, UserDTO userDTO) { public boolean login(HttpSession session, UserDTO userDTO) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment