Skip to content

Commit

Permalink
Merge branch 'master' into frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
ramory-l committed Jan 4, 2021
2 parents 1c0ab83 + 2aa5f87 commit 7a2d496
Show file tree
Hide file tree
Showing 26 changed files with 232 additions and 100 deletions.
5 changes: 5 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
<artifactId>cloudinary-http44</artifactId>
<version>${cloudinary.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.9</version>
</dependency>
</dependencies>

<build>
Expand Down
9 changes: 6 additions & 3 deletions backend/src/main/java/ru/school/matcha/MatchaApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ public static void main(String[] args) {
MailUtil.initMail();
CloudinaryAPI.init();
path(Path.API.getUrl(), () -> {
path(Path.AUTH.getUrl(), () ->
post("/login", AuthenticateController.authenticate, new JsonTransformer()));
path(Path.AUTH.getUrl(), () -> {
post("/login", AuthenticateController.authenticate, new JsonTransformer());
get("/check/password", AuthenticateController.checkPassword, new JsonTransformer());
});
post(Path.USERS.getUrl(), UserController.createUser, new JsonTransformer());
path(Path.USERS.getUrl(), () -> {
post("/batch", UserController.batchUsersCreate, new JsonTransformer());
Expand All @@ -41,6 +43,7 @@ public static void main(String[] args) {
get("/password/:hash", UserController.editPassword, new JsonTransformer());
get("/verified/:hash", UserController.verified, new JsonTransformer());
get("/matcha/:id", UserController.getMatcha, new JsonTransformer());
get("/search/:id", UserController.search, new JsonTransformer());
get("/messages/limit/:limit/offset/:offset/first/:first/second/:second", UserController.getMessages, new JsonTransformer());
get("/blacklist/:userId", UserController.getUserBlackList, new JsonTransformer());
put("/password/reset", UserController.resetPassword, new JsonTransformer());
Expand All @@ -61,7 +64,7 @@ public static void main(String[] args) {
});
path(Path.TAGS.getUrl(), () -> {
get("/:tagName", UserController.getUsersByTagName, new JsonTransformer());
get("/top/:count", TagController.getTopTags, new JsonTransformer());
get("/top/", TagController.getTopTags, new JsonTransformer());
});
path(Path.GUESTS.getUrl(), () -> post("/from/:from/to/:to", GuestController.createGuest, new JsonTransformer()));
path(Path.IMAGES.getUrl(), () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@
import ru.school.matcha.converters.AuthConverter;
import ru.school.matcha.converters.Converter;
import ru.school.matcha.domain.Auth;
import ru.school.matcha.domain.User;
import ru.school.matcha.dto.AuthDto;
import ru.school.matcha.enums.Response;
import ru.school.matcha.enums.Role;
import ru.school.matcha.serializators.Serializer;
import ru.school.matcha.services.AuthenticationServiceImpl;
import ru.school.matcha.services.UserServiceImpl;
import ru.school.matcha.services.interfaces.AuthenticationService;
import ru.school.matcha.services.interfaces.UserService;
import spark.Route;

import static spark.Spark.halt;

@Slf4j
public class AuthenticateController {

private static final Converter<AuthDto, Auth> authConverter = new AuthConverter();

private static final AuthenticationService authenticationService = new AuthenticationServiceImpl();
private static final UserService userService = new UserServiceImpl();

private static final Serializer<AuthDto> authDtoSerializer = new Serializer<>();

Expand All @@ -25,4 +33,17 @@ public class AuthenticateController {
return authenticationService.authenticate(authData.getUsername(), authData.getPassword());
};

public static Route checkPassword = (request, response) -> {
Long userId = AuthorizationController.authorize(request, Role.USER);
AuthDto authDto = authDtoSerializer.deserialize(request.body(), AuthDto.class);
Auth authData = authConverter.convertFromDto(authDto);
User user = userService.getUserByUsername(authData.getUsername());
if (userId != 0 && !user.getId().equals(userId)) {
halt(403, "Access is denied");
}
authenticationService.checkPassword(authData.getUsername(), authData.getPassword(), user);
response.status(Response.GET.getStatus());
return "OK";
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ public class TagController {
};

public static Route getTopTags = (request, response) -> {
Long count = parseLong(request.params("count"));
AuthorizationController.authorize(request, Role.USER);
List<Tag> tagList = tagService.getTopTags(count);
List<Tag> tagList = tagService.getTopTags();
response.status(Response.GET.getStatus());
return tagConverter.createFromEntities(tagList);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import lombok.extern.slf4j.Slf4j;
import ru.school.matcha.converters.*;
import ru.school.matcha.domain.Message;
import ru.school.matcha.domain.Tag;
import ru.school.matcha.domain.User;
import ru.school.matcha.domain.UserFullForBatch;
import ru.school.matcha.domain.*;
import ru.school.matcha.dto.*;
import ru.school.matcha.enums.Location;
import ru.school.matcha.enums.Response;
Expand All @@ -21,6 +18,7 @@
import spark.Route;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static java.lang.Integer.parseInt;
Expand All @@ -37,6 +35,7 @@ public class UserController {
private final static Converter<UserFullForBatchDto, UserFullForBatch> userFullForBatchConverter = new UserFullForBatchConverter();
private final static Converter<UserWithTagsDto, User> userWithTagsConverter = new UserWithTagsConverter();
private final static Converter<TagDto, Tag> tagConverter = new TagConverter();
private final static Converter<FormDto, Form> formConverter = new FormConverter();

private final static UserService userService = new UserServiceImpl();
private final static MessageService messageService = new MessageServiceImpl();
Expand Down Expand Up @@ -64,6 +63,34 @@ public class UserController {
return "";
};

public static Route search = (request, response) -> {
FormDto formDto = new FormDto(
null,
Boolean.parseBoolean(request.queryParams("man")),
Boolean.parseBoolean(request.queryParams("woman")),
parseInt(request.queryParams("agefrom")),
parseInt(request.queryParams("ageto")),
parseInt(request.queryParams("ratefrom")),
parseInt(request.queryParams("rateto")),
parseInt(request.queryParams("radius"))
);
String tags = request.queryParams("tags");
List<String> tagList = null;
if (tags != null && !tags.equals("")) {
tagList = Arrays.asList(tags.split(","));
}
long id = parseLong(request.params("id")),
userId = AuthorizationController.authorize(request, Role.USER);
if (userId != 0 && id != userId) {
halt(403, "Access is denied");
}
Form form = formConverter.convertFromDto(formDto);
List<User> users = userService.search(id, form, tagList);
List<UserDto> result = userConverter.createFromEntities(users);
response.status(Response.GET.getStatus());
return result;
};

public static Route getAllUsers = (request, response) -> {
long userId = AuthorizationController.authorize(request, Role.USER);
List<User> users = userService.getAllUsers(userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private static Auth convertToEntity(AuthDto source) {
return null;
}
if (isNull(source.getUsername()) || isNull(source.getPassword())) {
throw new MatchaException("Username or password exists");
throw new MatchaException("Username or password is incorrect");
}
Auth result = new Auth();
result.setUsername(source.getUsername());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ private static FormDto convertToDto(Form source) {
}
FormDto result = new FormDto();
result.setId(source.getId());
result.setFlirt(source.isFlirt());
result.setFriendship(source.isFriendship());
result.setLove(source.isLove());
result.setSex(source.isSex());
result.setMan(source.isMan());
result.setWoman(source.isWoman());
result.setAgeFrom(source.getAgeFrom());
result.setAgeTo(source.getAgeTo());
result.setRateFrom(source.getRateFrom());
result.setRateTo(source.getRateTo());
result.setRadius(source.getRadius());
return result;
}

Expand All @@ -34,14 +33,13 @@ private static Form convertToEntity(FormDto source) {
}
Form result = new Form();
result.setId(source.getId());
result.setFlirt(source.isFlirt());
result.setFriendship(source.isFriendship());
result.setLove(source.isLove());
result.setSex(source.isSex());
result.setMan(source.isMan());
result.setWoman(source.isWoman());
result.setAgeFrom(source.getAgeFrom());
result.setAgeTo(source.getAgeTo());
result.setRateFrom(source.getRateFrom());
result.setRateTo(source.getRateTo());
result.setRadius(source.getRadius());
return result;
}
}
2 changes: 1 addition & 1 deletion backend/src/main/java/ru/school/matcha/dao/TagMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface TagMapper {

List<Tag> getTags();

List<Tag> getTopTags(Long count);
List<Tag> getTopTags();

Optional<Tag> getTagByName(String name);

Expand Down
7 changes: 7 additions & 0 deletions backend/src/main/java/ru/school/matcha/dao/UserMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import ru.school.matcha.domain.Form;
import ru.school.matcha.domain.User;
import ru.school.matcha.domain.UserFullForBatch;

Expand All @@ -13,6 +14,12 @@ public interface UserMapper {

List<User> getAllUsers(Long userId);

List<User> search(
@Param("userId") long userId,
@Param("form") Form form,
@Param("tags") List<String> tags
);

Optional<User> getUserById(Long id);

Optional<User> getUserByEmail(String email);
Expand Down
7 changes: 3 additions & 4 deletions backend/src/main/java/ru/school/matcha/domain/Form.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ public class Form {
private Long id;
private boolean man;
private boolean woman;
private boolean friendship;
private boolean love;
private boolean sex;
private boolean flirt;
private Integer ageFrom;
private Integer ageTo;
private Integer rateFrom;
private Integer rateTo;
private Integer radius;

}
11 changes: 7 additions & 4 deletions backend/src/main/java/ru/school/matcha/dto/FormDto.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package ru.school.matcha.dto;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonAutoDetect
public class FormDto {

private Long id;
private boolean man;
private boolean woman;
private boolean friendship;
private boolean love;
private boolean sex;
private boolean flirt;
private Integer ageFrom;
private Integer ageTo;
private Integer rateFrom;
private Integer rateTo;
private Integer radius;

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,24 @@ public class AuthenticationServiceImpl implements AuthenticationService {
private static final JwtTokenProvider jwtTokenProvider = new JwtTokenProvider();

@Override
public String authenticate(String username, String password) throws InvalidKeySpecException, NoSuchAlgorithmException, AuthenticationException {
public String authenticate(String username, String password) throws AuthenticationException, InvalidKeySpecException, NoSuchAlgorithmException {
User user = userService.getUserByUsername(username);
if (isNull(user)) {
throw new AuthenticationException(String.format("User with username: %s not found", username));
}
if (!user.getIsVerified()) {
throw new AuthenticationException(String.format("User with username: %s not verified", username));
}
checkPassword(username, password, user);
return jwtTokenProvider.createToken(user.getId(), user.getUsername(), user.getRole());
}

@Override
public void checkPassword(String username, String password, User user) throws InvalidKeySpecException, NoSuchAlgorithmException, AuthenticationException {
String encryptedPassword = userService.getUserEncryptPasswordById(user.getId());
if (!PasswordCipher.validatePassword(password, encryptedPassword)) {
throw new AuthenticationException("Users password is wrong");
}
return jwtTokenProvider.createToken(user.getId(), user.getUsername(), user.getRole());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ public void like(Long from, Long to, boolean isLike) {
}

private boolean checkAbilityLike(long userId) {
return imageService.getCountImagesByUserId(userId) != 0;
if (userId == 1) {
return true;
}
return imageService.getCountImagesByUserId(userId) != 0 && nonNull(userService.getUserById(userId).getAvatar());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ public void deleteUserRefTag(String tagName, Long userId) {
}

@Override
public List<Tag> getTopTags(Long count) {
public List<Tag> getTopTags() {
try (SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession()) {
TagMapper tagMapper = sqlSession.getMapper(TagMapper.class);
return tagMapper.getTopTags(count);
return tagMapper.getTopTags();
}
}

Expand Down
Loading

0 comments on commit 7a2d496

Please sign in to comment.