Skip to content
Snippets Groups Projects
Commit 2eb1eec6 authored by Joshua Gill's avatar Joshua Gill
Browse files

All Tests Passing

parent b060ccfa
No related branches found
No related tags found
4 merge requests!114LoggingService service class, new method to add a log to the "Logs" table when...,!74User is now authenticated on post request, all fields functional on client...,!68merge,!67Issue Complete
......@@ -3,60 +3,12 @@ package com.example.clientproject.data.misc;
import com.example.clientproject.data.tags.Tags;
import com.example.clientproject.data.users.Users;
import com.example.clientproject.service.dtos.UsersDTO;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Class for any misc queries, JDBC will be used instead of JPA for more customization
*/
@Repository
public class MiscQueries {
private final JdbcTemplate jdbcTemplate;
private final RowMapper<UserFavouriteTags> userFavouriteTagsRowMapper;
/**
* Constructor
* @param aJdbcTemplate - the JDBC Template to pass in
*/
public MiscQueries(JdbcTemplate aJdbcTemplate) {
this.jdbcTemplate = aJdbcTemplate;
userFavouriteTagsRowMapper = (rs, i) -> new UserFavouriteTags(
rs.getLong("User_Favourite_Tag_Id"),
rs.getLong("User_Id"),
rs.getLong("Tag_Id")
);
}
/**
* Insert into "User_Favourite_Tags"
* @param userDTO - the user as a DTO object
* @param tag - the tag
*/
public void saveUserFavouriteTags(UsersDTO userDTO, Tags tag) {
SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate)
.withTableName("User_Favourite_Tage")
.usingGeneratedKeyColumns("User_Favourite_Tag_Id");
Map<String, Object> parameters = new HashMap<>();
parameters.put("User_Id", userDTO.getUserId());
parameters.put("Tag_Id", tag.getTagId());
Number id = simpleJdbcInsert.execute(parameters);
}
public List<UserFavouriteTags> findAllUserFavouriteTags() {
return jdbcTemplate.query(
"select * from User_Favourite_Tags",
userFavouriteTagsRowMapper,
new Object[]{}
);
}
public interface MiscQueries {
void saveUserFavouriteTags(Users user, Tags tag);
void saveUser(Users user);
void saveTag(Tags tag);
List<UserFavouriteTags> findAllUserFavouriteTags();
}
package com.example.clientproject.data.misc;
import com.example.clientproject.data.tags.Tags;
import com.example.clientproject.data.users.Users;
import com.example.clientproject.service.dtos.UsersDTO;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Class for any misc queries, JDBC will be used instead of JPA for more customization
*/
@Repository
public class MiscQueriesImpl implements MiscQueries{
private final JdbcTemplate jdbcTemplate;
private final RowMapper<UserFavouriteTags> userFavouriteTagsRowMapper;
/**
* Constructor
* @param aJdbcTemplate - the JDBC Template to pass in
*/
public MiscQueriesImpl(JdbcTemplate aJdbcTemplate) {
this.jdbcTemplate = aJdbcTemplate;
userFavouriteTagsRowMapper = (rs, i) -> new UserFavouriteTags(
rs.getLong("User_Favourite_Tag_Id"),
rs.getLong("User_Id"),
rs.getLong("Tag_Id")
);
}
/**
* Insert into "User_Favourite_Tags"
* @param user - the user
* @param tag - the tag
*/
public void saveUserFavouriteTags(Users user, Tags tag) {
SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate)
.withTableName("User_Favourite_Tags")
.usingGeneratedKeyColumns("User_Favourite_Tag_Id");
Map<String, Object> parameters = new HashMap<>();
parameters.put("User_Id", user.getUserId());
parameters.put("Tag_Id", tag.getTagId());
Number id = simpleJdbcInsert.execute(parameters);
}
/**
* Insert into "Users" table
* @param user - the user to insert
*/
public void saveUser(Users user) {
SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate)
.withTableName("Users")
.usingGeneratedKeyColumns("User_Id");
Map<String, Object> parameters = new HashMap<>();
parameters.put("User_Id", user.getUserId());
parameters.put("User_First_Name", user.getUserFirstName());
parameters.put("User_Last_Name", user.getUserLastName());
parameters.put("User_Email", user.getUserEmail());
parameters.put("User_Password", user.getUserPassword());
parameters.put("User_Profile_Picture", user.getUserProfilePicture());
parameters.put("Two_Factor_Method_Id", user.getTwoFactorMethod().getTwoFactorMethodId());
parameters.put("User_Reset_Code", user.getUserResetCode());
parameters.put("User_Reset_Code_Expiry", user.getUserResetCodeExpiry());
Number id = simpleJdbcInsert.execute(parameters);
}
/**
* Insert into the "Tags" table
* @param tag - the tag to insert
*/
public void saveTag(Tags tag) {
SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate)
.withTableName("Tags")
.usingGeneratedKeyColumns("Tag_Id");
Map<String, Object> parameters = new HashMap<>();
parameters.put("Tag_Id", tag.getTagId());
parameters.put("Tag_Name", tag.getTagName());
Number id = simpleJdbcInsert.execute(parameters);
}
/**
* Find all entries into the "User_Favourite_Tags" table
* @return - list of all entries found
*/
public List<UserFavouriteTags> findAllUserFavouriteTags() {
return jdbcTemplate.query(
"select * from User_Favourite_Tags",
userFavouriteTagsRowMapper,
new Object[]{}
);
}
}
......@@ -5,6 +5,7 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
import java.util.Optional;
/**
* JPA Repository for the "Tags" Entity
......@@ -20,4 +21,7 @@ public interface TagsRepo extends JpaRepository<Tags, Long> {
@Query("select t.tagId from Tags t where t.tagId = (select max(t.tagId) from Tags t)")
int findMostRecent();
@Query("select t from Tags t where t.tagName = ?1")
Optional<Tags> findByTagName(String tagName);
}
......@@ -14,6 +14,7 @@ import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.jdbc.Sql;
......@@ -25,7 +26,7 @@ import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
@DataJpaTest
@SpringBootTest
@Sql(scripts={"/schema-test-h2.sql","/script-test-h2.sql"})
@ActiveProfiles("h2")
@DirtiesContext
......@@ -33,9 +34,9 @@ public class SelectCategoriesTests {
@Autowired
TwoFactorMethodsRepo twoFactorMethodsRepo;
@Autowired
TagsRepo tagsRepo;
UsersRepo usersRepo;
@Autowired
UsersSearch usersSearch;
TagsRepo tagsRepo;
@Autowired
MiscQueries miscQueries;
......@@ -57,9 +58,9 @@ public class SelectCategoriesTests {
"", "",
LocalDateTime.now().format(formatter), twoFactorMethods);
// Save the user
Users users = usersSearch.save(newUser);
miscQueries.saveUser(newUser);
// Get the user as a DTO object
Optional<UsersDTO> usersDTOOptional = usersSearch.findByEmail(newUser.getUserEmail());
Optional<Users> usersOptional = usersRepo.findByUserEmail(newUser.getUserEmail());
// Split the array of tags by a comma
String[] tagNames = tags.split(",");
......@@ -68,9 +69,11 @@ public class SelectCategoriesTests {
// Create a new "Tags" object with that name
Tags newTag = new Tags(tagName);
// Save a new tag with that name
tagsRepo.save(newTag);
miscQueries.saveTag(newTag);
// Get the newly saved tag
Optional<Tags> tagsOptional = tagsRepo.findByTagName(tagName);
// Add a row to the "User_Favourite_Tags" table
miscQueries.saveUserFavouriteTags(usersDTOOptional.get(), newTag);
miscQueries.saveUserFavouriteTags(usersOptional.get(), tagsOptional.get());
}
// Get the size of the table at the beginning
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment