Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #52 from mMosiur/develop
Browse files Browse the repository at this point in the history
Few small fixes
  • Loading branch information
mMosiur authored Jun 9, 2022
2 parents 1027f55 + e58976c commit f990618
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 49 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Więcej szczegółowych informacji o naszej aplikacji w folderze z dokumentacją
- Backend zdeployowany do Azure Web Apps:

[https://umcs-appollo.azurewebsites.net](https://umcs-appollo.azurewebsites.net/)

Swagger UI: [https://umcs-appollo.azurewebsites.net/swagger-ui](https://umcs-appollo.azurewebsites.net/swagger-ui/)

- Baza danych (PostgreSQL) zdeployowana do Azure.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.umcs.appollo.controllers;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -15,15 +15,14 @@
import org.umcs.appollo.api.UsersApi;
import org.umcs.appollo.configuration.RoleNames;
import org.umcs.appollo.configuration.TokenProvider;
import org.umcs.appollo.exceptions.ConflictException;
import org.umcs.appollo.model.RoleEntity;
import org.umcs.appollo.model.UserEntity;
import org.umcs.appollo.model.api.AuthToken;
import org.umcs.appollo.model.api.User;
import org.umcs.appollo.model.api.UserLoginAttempt;
import org.umcs.appollo.services.UserService;

import java.util.Set;

@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/api/")
Expand Down Expand Up @@ -66,6 +65,8 @@ public ResponseEntity<User> createUser(User user) {
}
catch (ResponseStatusException e){
throw new RuntimeException(e.getMessage());
} catch (DataIntegrityViolationException e) {
throw new ConflictException(e.getMessage());
}

return new ResponseEntity<>(result, HttpStatus.CREATED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.stereotype.Service;
import org.umcs.appollo.model.PollEntity;
import org.umcs.appollo.model.QuestionEntity;
import org.umcs.appollo.model.api.Poll;
import org.umcs.appollo.model.api.PollLabel;
import org.umcs.appollo.model.api.Question;
Expand Down Expand Up @@ -47,11 +46,6 @@ public Poll FromEntityToApiDetailed(PollEntity pollEntity) {
public PollEntity FromApiToEntity(Poll poll) {
PollEntity pollEntity = new PollEntity();
pollEntity.setId(poll.getId());
List<QuestionEntity> questions = poll.getQuestions()

This comment has been minimized.

Copy link
@mMosiur

mMosiur Jun 9, 2022

Author Owner

To jest potencjalnie ta zmiana która zepsuła poll create?

.stream()
.map(questionConverter::FromApiToEntity)
.collect(Collectors.toList());
pollEntity.setQuestions(questions);
pollEntity.setName(poll.getName());
return pollEntity;
}
Expand Down
9 changes: 3 additions & 6 deletions backend/src/main/java/org/umcs/appollo/model/PollEntity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.umcs.appollo.model;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity(name = "Poll")
Expand All @@ -13,15 +14,15 @@ public class PollEntity {
@Column(name = "name")
private String name;


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private UserEntity user;

@OneToMany(mappedBy = "poll", cascade = CascadeType.ALL)
@OneToMany(mappedBy = "poll", cascade = CascadeType.ALL, orphanRemoval = true)
private List<QuestionEntity> questions;

public PollEntity() {
this.questions = new ArrayList<>();
}

public PollEntity(String name) {
Expand Down Expand Up @@ -55,8 +56,4 @@ public void setUser(UserEntity user) {
public List<QuestionEntity> getQuestions() {
return questions;
}

public void setQuestions(List<QuestionEntity> questions) {
this.questions = questions;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.umcs.appollo.model;

import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import javax.persistence.*;
import java.util.List;

Expand All @@ -21,6 +24,7 @@ public class QuestionEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="poll_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private PollEntity poll;

@OneToMany(mappedBy = "question", cascade = CascadeType.ALL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
public interface UserRepository extends JpaRepository<UserEntity, Integer> {
@Query( value = "SELECT u from Users u where u.username = ?1")
UserEntity findByUsername(String username);

@Query(value = "SELECT u FROM Users u where u.email = ?1")
UserEntity findByEmail(String email);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.umcs.appollo.repository.QuestionRepository;
import org.umcs.appollo.repository.UserRepository;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -57,7 +56,6 @@ public PollEntity createPoll(Poll poll, int owner_id) {
PollEntity pollEntity = pollConverter.FromApiToEntity(poll);
pollEntity.setId(null);
pollEntity.setUser(userRepository.getById(owner_id));
pollEntity.setQuestions(null);
pollEntity = pollRepository.save(pollEntity);
createAndSetQuestionEntitiesToPollEntity(poll, pollEntity);
return pollRepository.save(pollEntity);
Expand All @@ -76,20 +74,19 @@ public Poll updatePoll(Integer id, Poll newPoll) {
throw new ResourceNotFoundException( "No poll of id " + id + " found.");

PollEntity target = pollRepository.getById(id);
target.getQuestions().clear();
target = pollRepository.save(target);

questionRepository.deleteAll(target.getQuestions());
createAndSetQuestionEntitiesToPollEntity(newPoll, target);

return pollConverter.FromEntityToApiDetailed(pollRepository.save(target));
}

private void createAndSetQuestionEntitiesToPollEntity(Poll newPoll, PollEntity target) {
List<QuestionEntity> questionEntities = new ArrayList<>();
List<QuestionEntity> questionEntities = target.getQuestions();
for (Question question: newPoll.getQuestions()) {
QuestionEntity questionEntity = questionConverter.FromApiToEntity(question);
questionEntity.setPoll(target);
questionEntities.add(questionEntity);
}
target.setQuestions(questionEntities);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.web.server.ResponseStatusException;
import org.umcs.appollo.configuration.RoleNames;
import org.umcs.appollo.converters.UserConverter;
import org.umcs.appollo.exceptions.ConflictException;
import org.umcs.appollo.exceptions.ResourceNotFoundException;
import org.umcs.appollo.model.RoleEntity;
import org.umcs.appollo.model.UserEntity;
Expand Down Expand Up @@ -57,6 +58,14 @@ private Set<SimpleGrantedAuthority> getAuthority(UserEntity user) {
@Override
public User addNewUser(User user) {
UserEntity userEntity = userConverter.FromApiToEntity(user);

UserEntity a = userRepository.findByUsername(user.getUsername());
if (a != null)
throw new ConflictException("Username is already taken.");
a = userRepository.findByEmail(user.getEmail());
if (a != null)
throw new ConflictException("Email is already taken.");

userEntity.setId(null);
userEntity.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
Set<RoleEntity> roles = new HashSet<>();
Expand Down Expand Up @@ -115,6 +124,14 @@ public User edit(int id, User data) {
catch (NoSuchElementException e) {
throw new ResourceNotFoundException("No user of id " + id + " found.", e.getCause());
}

UserEntity a = userRepository.findByUsername(data.getUsername());
if (a != null)
throw new ConflictException("Username already taken.");
a = userRepository.findByEmail(data.getEmail());
if (a != null)
throw new ConflictException("Email already taken.");

target.setUsername(data.getUsername());
target.setPassword(bCryptPasswordEncoder.encode(data.getPassword()));
target.setEmail(data.getEmail());
Expand Down
10 changes: 7 additions & 3 deletions backend/src/main/resources/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ info:
description: >-
ApPOLLo API enables creation and management of polls, answers and app's
users.
version: 0.0.3
version: 0.0.9
paths:
/polls:
post:
Expand Down Expand Up @@ -207,6 +207,8 @@ paths:
$ref: '#/components/schemas/User'
'400':
description: Bad request body.
'409':
description: Conflicting user data.
/users/{id}:
get:
description: Get user of a given id.
Expand Down Expand Up @@ -268,6 +270,8 @@ paths:
description: User not authorized to edit this user.
'404':
description: User not found.
'409':
description: Conflicting user data.
delete:
description: Delete user of a given id.
operationId: deleteUserById
Expand Down Expand Up @@ -342,7 +346,7 @@ components:
properties:
id:
type: integer
example: 1234
example: 1
readOnly: true
text:
type: string
Expand Down Expand Up @@ -371,7 +375,7 @@ components:
properties:
id:
type: integer
example: 1234
example: 1
readOnly: true
question_id:
type: integer
Expand Down
21 changes: 0 additions & 21 deletions backend/src/test/java/org/umcs/appollo/AppolloConverterTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,6 @@ public void setup() {
user1.setLastname(lastName1);

user2Entity = new UserEntity();
// user2Entity.setId(user2Id);
// user2Entity.setUsername(username2);
// user2Entity.setPassword(password2);
// user2Entity.setEmail(email2);
// user2Entity.setFirstName(firstName2);
// user2Entity.setLastName(lastName2);
// user2 = new User();
// user2.setId(user2Id);
// user2.setUsername(username2);
// user2.setPassword(password2);
// user2.setEmail(email2);
// user2.setFirstname(firstName2);
// user2.setLastname(lastName2);

// Model a poll
pollEntity.setId(pollId);
Expand Down Expand Up @@ -126,16 +113,11 @@ public void setup() {
answer.setAnswerJson(answerJson);

// Connect relations
List<QuestionEntity> questionEntitiesList = new LinkedList<>();
questionEntitiesList.add(questionEntity);
List<Question> questionDetailsList = new LinkedList<>();
questionDetailsList.add(question);
List<AnswerEntity> answerEntitiesList = new LinkedList<>();
answerEntitiesList.add(answerEntity);
List<Answer> answerDetailsList = new LinkedList<>();
answerDetailsList.add(answer);

pollEntity.setQuestions(questionEntitiesList);
poll.setQuestions(questionDetailsList);
questionEntity.setPoll(pollEntity);
questionEntity.setAnswers(answerEntitiesList);
Expand Down Expand Up @@ -168,8 +150,6 @@ public void ConvertPollFromEntityToApiDetailed() {
Assert.assertEquals(poll.getId().intValue(), pollId);
Assert.assertEquals(poll.getName(), pollName);
Assert.assertEquals(poll.getIsActive(), true);
Assert.assertEquals(poll.getQuestions().size(), 1);
Assert.assertEquals(poll.getQuestions().get(0), question);
}

@Test
Expand All @@ -187,7 +167,6 @@ public void ConvertPollFromApiToEntity() {
PollEntity pollEntity = pollConverter.FromApiToEntity(poll);
Assert.assertEquals(pollEntity.getId().intValue(), pollId);
Assert.assertEquals(pollEntity.getName(), pollName);
Assert.assertEquals(pollEntity.getQuestions().size(), 1);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ public void setup(){
List<AnswerEntity> answerEntitiesList = new LinkedList<>();
answerEntitiesList.add(answerEntity);

pollEntity.setQuestions(questionEntitiesList);
pollDetails.setQuestions(questionDetailsList);
questionEntity.setAnswers(answerEntitiesList);
questionEntity.setPoll(pollEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,9 @@ public void setup(){
"Red", "Green", "Blue"
}));

List<QuestionEntity> questionEntitiesList = new LinkedList<>();
questionEntitiesList.add(questionEntity);
List<Question> questionDetailsList = new LinkedList<>();
questionDetailsList.add(questionConverter.FromEntityToApi(questionEntity));

pollEntity.setQuestions(questionEntitiesList);
pollDetails.setQuestions(questionDetailsList);
}

Expand Down Expand Up @@ -125,6 +122,7 @@ public void deletePollCorrect(){
@Test
public void updatePollCorrect(){
given(pollRepository.existsById(1)).willReturn(true);
given(pollRepository.getById(1)).willReturn(pollEntity);
pollDetails.setName("test123");

when(pollRepository.save(any(PollEntity.class))).thenReturn(pollConverter.FromApiToEntity(pollDetails));
Expand Down

0 comments on commit f990618

Please sign in to comment.