Skip to content
Snippets Groups Projects
Commit 6ac3ce2d authored by Thomas's avatar Thomas
Browse files

Kind of fixed search

parent 7fa24200
Branches
No related tags found
No related merge requests found
...@@ -7,6 +7,7 @@ import org.springframework.web.multipart.MultipartFile; ...@@ -7,6 +7,7 @@ import org.springframework.web.multipart.MultipartFile;
import mozen.messages.ModelMessage; import mozen.messages.ModelMessage;
import mozen.messages.TagMessage; import mozen.messages.TagMessage;
import mozen.model.Model; import mozen.model.Model;
import mozen.messages.SearchResult;
import mozen.model.TagCategory; import mozen.model.TagCategory;
import mozen.model.User; import mozen.model.User;
...@@ -23,5 +24,7 @@ public interface IModelManager { ...@@ -23,5 +24,7 @@ public interface IModelManager {
Model getModel(long id); Model getModel(long id);
Collection<TagCategory> getTags(); Collection<TagCategory> getTags();
Collection<Model> findModel(String name); SearchResult findModel(String name, int page, int size, String sort);
SearchResult findModelWithTags(String name, int page, int size, String sort, Collection<String> tags);
} }
\ No newline at end of file
package mozen.business; package mozen.business;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
...@@ -14,6 +17,7 @@ import mozen.messages.ModelMessage; ...@@ -14,6 +17,7 @@ import mozen.messages.ModelMessage;
import mozen.messages.TagMessage; import mozen.messages.TagMessage;
import mozen.model.CustomLayer; import mozen.model.CustomLayer;
import mozen.model.Model; import mozen.model.Model;
import mozen.messages.SearchResult;
import mozen.model.Tag; import mozen.model.Tag;
import mozen.model.TagCategory; import mozen.model.TagCategory;
import mozen.model.User; import mozen.model.User;
...@@ -111,29 +115,25 @@ public class ModelManager implements IModelManager { ...@@ -111,29 +115,25 @@ public class ModelManager implements IModelManager {
} }
@Override @Override
public Collection<Model> findModel(String name) { public SearchResult findModel(String name, int page, int size, String sort) {
Collection<Model> result = modelRepo.findByName(name); System.err.println("[MODEL MANAGER] search n:"+name+" p:"+page+" si:"+size+" so:"+sort);
Pageable paging = PageRequest.of(page-1, size, Sort.by(sort));
return result; Page<Model> resultPage = modelRepo.findByNameContainingIgnoreCase(name, paging);
System.err.println("[MODEL MANAGER] search result "+resultPage.getTotalElements());
SearchResult result = new SearchResult();
if (resultPage.hasContent()) {
result.setTotalResult((int) resultPage.getTotalElements());
result.setTotalPage((int) resultPage.getTotalPages());
result.setPage(resultPage.getNumber()+1);
result.setModels(resultPage.getContent());
} else {
result.setPage(0);
result.setTotalResult(0);
result.setTotalPage(0);
} }
return result;
@Override
public Collection<TagCategory> getTags() {
Collection<TagCategory> categories = new ArrayList<TagCategory>();
tagCategoryRepo.findAll().forEach(categories::add);
return categories;
}
@Override
public void removeLayer(long id, User user) throws Exception {
CustomLayer layer = layerRepo.findById(id).get();
if (layer == null)
throw new Exception("Unknown layer");
if (!isLayerAuthor(layer, user))
throw new Exception("Not the author");
layerRepo.deleteById(id);
} }
@Override @Override
...@@ -155,12 +155,16 @@ public class ModelManager implements IModelManager { ...@@ -155,12 +155,16 @@ public class ModelManager implements IModelManager {
modelRepo.save(model); modelRepo.save(model);
} }
private boolean isLayerAuthor(CustomLayer layer, User user) { @Override
return layer.getModel().getAuthor() == user; public void removeLayer(long id, User user) throws Exception {
} CustomLayer layer = layerRepo.findById(id).get();
private boolean isModelAuthor(Model model, User user) { if (layer == null)
return model.getAuthor() == user; throw new Exception("Unknown layer");
if (!isLayerAuthor(layer, user))
throw new Exception("Not the author");
layerRepo.deleteById(id);
} }
@Override @Override
...@@ -176,4 +180,42 @@ public class ModelManager implements IModelManager { ...@@ -176,4 +180,42 @@ public class ModelManager implements IModelManager {
tagRepo.save(tag); tagRepo.save(tag);
} }
@Override
public Collection<TagCategory> getTags() {
// Collection<TagCategory> categories = new ArrayList<TagCategory>();
// tagCategoryRepo.findAll().forEach(categories::add);
return tagCategoryRepo.findAll();
}
private boolean isLayerAuthor(CustomLayer layer, User user) {
return layer.getModel().getAuthor() == user;
}
private boolean isModelAuthor(Model model, User user) {
return model.getAuthor() == user;
}
@Override
public SearchResult findModelWithTags(String name, int page, int size, String sort, Collection<String> tagsName) {
System.err.println("[MODEL MANAGER] search with tags n:"+name+" p:"+page+" si:"+size+" so:"+sort+" t:"+tagsName);
Pageable paging = PageRequest.of(page-1, size, Sort.by(sort));
Collection<Tag> tags = tagRepo.findByNameIn(tagsName);
Page<Model> resultPage = modelRepo.findByNameContainingIgnoreCaseAndTagsIn(name, tags, paging);
System.err.println("[MODEL MANAGER] search result "+resultPage.getTotalElements());
SearchResult result = new SearchResult();
if (resultPage.hasContent()) {
result.setTotalResult((int) resultPage.getTotalElements());
result.setTotalPage((int) resultPage.getTotalPages());
result.setPage(resultPage.getNumber()+1);
result.setModels(resultPage.getContent());
} else {
result.setPage(0);
result.setTotalResult(0);
result.setTotalPage(0);
}
return result;
}
} }
\ No newline at end of file
package mozen.business; package mozen.business;
import java.util.List; import java.util.Collection;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.PagingAndSortingRepository;
import mozen.model.Model; import mozen.model.Model;
import mozen.model.Tag;
public interface ModelRepository extends PagingAndSortingRepository<Model, Long> { public interface ModelRepository extends PagingAndSortingRepository<Model, Long> {
List<Model> findByName(String name); Page<Model> findByNameContainingIgnoreCase(String name, Pageable pageable);
Page<Model> findByNameContainingIgnoreCaseAndTagsIn(String name, Collection<Tag> tags, Pageable pageable);
// @Query("SELECT m FROM Model m WHERE m.name LIKE :name AND m.tags CONTAINS (SELECT t FROM Tag WHERE t.name IN :tags)") <- horrible et c'est même pas complet
// Page<Model> findByNameContainingIgnoreCaseTags(@Param("name") String name, @Param("tags") Collection<String> tags, Pageable pageable);
} }
\ No newline at end of file
package mozen.business; package mozen.business;
import java.util.List;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import mozen.model.TagCategory; import mozen.model.TagCategory;
public interface TagCategoryRepository extends CrudRepository<TagCategory, Long>{ public interface TagCategoryRepository extends CrudRepository<TagCategory, Long>{
List<TagCategory> findAll();
} }
\ No newline at end of file
package mozen.business; package mozen.business;
import java.util.Collection;
import java.util.List; import java.util.List;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
...@@ -8,4 +9,5 @@ import mozen.model.Tag; ...@@ -8,4 +9,5 @@ import mozen.model.Tag;
public interface TagRepository extends CrudRepository<Tag, Long>{ public interface TagRepository extends CrudRepository<Tag, Long>{
List<Tag> findByName(String name); List<Tag> findByName(String name);
List<Tag> findByNameIn(Collection<String> name);
} }
\ No newline at end of file
package mozen.model; package mozen.messages;
import java.io.Serializable; import java.io.Serializable;
import java.util.Collection; import java.util.Collection;
import mozen.model.Model;
public class SearchResult implements Serializable { public class SearchResult implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private int total;
private int page; private int page;
private int totalResult;
private int totalPage;
private Collection<Model> models; private Collection<Model> models;
public SearchResult() { public SearchResult() {
} }
public SearchResult(int total, int page, Collection<Model> models) { public SearchResult(int page, int totalResult, int totalPage, Collection<Model> models) {
this.total = total;
this.page = page; this.page = page;
this.totalResult = totalResult;
this.totalPage = totalPage;
this.models = models; this.models = models;
} }
public int getTotal() {
return this.total;
}
public void setTotal(int total) {
this.total = total;
}
public int getPage() { public int getPage() {
return this.page; return this.page;
} }
...@@ -36,6 +31,22 @@ public class SearchResult implements Serializable { ...@@ -36,6 +31,22 @@ public class SearchResult implements Serializable {
this.page = page; this.page = page;
} }
public int getTotalResult() {
return this.totalResult;
}
public void setTotalResult(int totalResult) {
this.totalResult = totalResult;
}
public int getTotalPage() {
return this.totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public Collection<Model> getModels() { public Collection<Model> getModels() {
return this.models; return this.models;
} }
......
package mozen.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import mozen.business.IModelManager;
import mozen.business.IUserManager;
@RestController
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {
@Autowired
IModelManager modelManager;
@Autowired
IUserManager userManager;
}
\ No newline at end of file
...@@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.RequestParam; ...@@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import mozen.business.IModelManager; import mozen.business.IModelManager;
import mozen.model.SearchResult; import mozen.messages.SearchResult;
@RestController @RestController
@RequestMapping("/search") @RequestMapping("/search")
...@@ -21,18 +21,19 @@ public class SearchController { ...@@ -21,18 +21,19 @@ public class SearchController {
@GetMapping("") @GetMapping("")
public SearchResult search( public SearchResult search(
@RequestParam(value = "name", required = false) String name, @RequestParam(value = "name", required = false, defaultValue = "") String name,
@RequestParam(value = "tag", required = false) Collection<String> tag, @RequestParam(value = "tags", required = false) Collection<String> tags,
@RequestParam(value = "param", required = false) Integer param, @RequestParam(value = "param", required = false) Integer param,
@RequestParam(value = "size", required = true) Integer size, @RequestParam(value = "size", required = true) Integer size,
@RequestParam(value = "page", required = true) Integer page, @RequestParam(value = "page", required = true) Integer page,
@RequestParam(value = "sort", required = false) String sort @RequestParam(value = "sort", required = false, defaultValue = "votes") String sort
) )
{ {
SearchResult result = new SearchResult(); SearchResult result;
result.setModels(modelManager.findModel(name));
result.setTotal(result.getModels().size()); if (tags.isEmpty()) result = modelManager.findModel(name, page, size, sort);
result.setPage(page); else result = modelManager.findModelWithTags(name, page, size, sort, tags);
return result; return result;
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment