Skip to content
Permalink
Browse files

Added services

  • Loading branch information...
borabilgin committed Jan 26, 2019
1 parent 6bbf05c commit c02ba55d69d76e63d28b619f1d87415b79662a6b
@@ -5,4 +5,5 @@

public interface FeedCategoryRepository extends CrudRepository<FeedCategory, Long> {
FeedCategory findByName(String name);
void deleteByName(String name);
}
@@ -0,0 +1,13 @@
package com.demo.imagebrowser.service;

import com.demo.imagebrowser.domain.Feed;
import com.demo.imagebrowser.domain.FeedCategory;

import java.util.List;

public interface FeedService {
List<Feed> findByCategoryName(String categoryName);
void deleteCategoryByName(String categoryName);
FeedCategory addCategory(String categoryName);
Feed addFeed(Feed feed);
}
@@ -0,0 +1,48 @@
package com.demo.imagebrowser.service.impl;

import com.demo.imagebrowser.domain.Feed;
import com.demo.imagebrowser.domain.FeedCategory;
import com.demo.imagebrowser.repository.FeedCategoryRepository;
import com.demo.imagebrowser.repository.FeedRepository;
import com.demo.imagebrowser.service.FeedService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.List;

public class FeedServiceImpl implements FeedService {

@Autowired
private FeedRepository feedRepository;

@Autowired
private FeedCategoryRepository feedCategoryRepository;

@Override
public List<Feed> findByCategoryName(String categoryName) {
FeedCategory category = feedCategoryRepository.findByName(categoryName);
if (category != null) {
return feedRepository.findByCategory(category);
} else {
return Collections.emptyList();
}
}

@Override
public void deleteCategoryByName(String categoryName) {
feedCategoryRepository.deleteByName(categoryName);
}

@Override
public FeedCategory addCategory(String categoryName) {
FeedCategory category = new FeedCategory();
category.setName(categoryName);
feedCategoryRepository.save(category);
return category;
}

@Override
public Feed addFeed(Feed feed) {
return feedRepository.save(feed);
}
}
@@ -0,0 +1,35 @@
package com.demo.imagebrowser.service.impl;

import com.demo.imagebrowser.domain.User;
import com.demo.imagebrowser.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

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

@Component
public class UserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService {

@Autowired
private UserRepository userRepository;

@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
User user = userRepository.findByUsername(s);

if (user == null) {
throw new UsernameNotFoundException(String.format("User with name %s does not exists", s));
}

List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
user.getRoles().forEach(r -> grantedAuthorities.add(new SimpleGrantedAuthority(r.getRoleName())));

UserDetails details = new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), grantedAuthorities);
return details;
}
}

0 comments on commit c02ba55

Please sign in to comment.
You can’t perform that action at this time.