Commit c13eaf13 authored by Khalid Ali's avatar Khalid Ali

#2 Add ShareUser

parent bc47e412
......@@ -16,7 +16,11 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.logout.LogoutFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import java.util.Arrays;
import java.util.Collections;
@Configuration
......@@ -40,6 +44,10 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors()
.and()
.csrf()
.disable()
.authorizeRequests()
.regexMatchers("/secured.*", "/login", "/bs/api/.*")
.authenticated()
......@@ -74,4 +82,16 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
filter.setAuthenticationManager(authenticationManager());
return filter;
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("*"));
configuration.setAllowedMethods(Arrays.asList("*"));
configuration.setAllowedHeaders(Arrays.asList("*"));
configuration.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}
\ No newline at end of file
package com.gmu.bookshare.model;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ShareUserDto {
@JsonProperty("email")
private String email;
@JsonProperty("name")
private String name;
}
......@@ -3,6 +3,9 @@ package com.gmu.bookshare.persistence;
import com.gmu.bookshare.entity.ShareUser;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
public interface ShareUserRepository extends CrudRepository<ShareUser, Long> {
List<ShareUser> findByEmail(String s);
}
package com.gmu.bookshare.service;
import com.gmu.bookshare.entity.ShareUser;
import com.gmu.bookshare.persistence.ShareUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ShareUserService {
......@@ -13,4 +16,14 @@ public class ShareUserService {
public ShareUserService(ShareUserRepository shareUserRepository) {
this.shareUserRepository = shareUserRepository;
}
public ShareUser addShareUser(ShareUser user) {
List<ShareUser> userList = shareUserRepository.findByEmail(user.getEmail());
if (userList.size() > 0) {
return userList.get(0);
} else {
shareUserRepository.save(user);
return user;
}
}
}
......@@ -2,8 +2,10 @@ package com.gmu.bookshare.web;
import com.gmu.bookshare.entity.BidEntity;
import com.gmu.bookshare.entity.ListingEntity;
import com.gmu.bookshare.entity.ShareUser;
import com.gmu.bookshare.model.BidDto;
import com.gmu.bookshare.model.ListingDto;
import com.gmu.bookshare.model.ShareUserDto;
import com.gmu.bookshare.service.BidService;
import com.gmu.bookshare.service.ListingService;
import com.gmu.bookshare.service.ShareUserService;
......@@ -99,6 +101,14 @@ public class BookshareApiController {
return convertBidToDto(bidCreated);
}
@PostMapping(value = "/user", consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public ShareUserDto newListing(@RequestBody ShareUserDto shareUserDto) {
ShareUser user = convertShareUserToEntity(shareUserDto);
ShareUser userCreated = shareUserService.addShareUser(user);
return convertShareUserToDto(userCreated);
}
@GetMapping(name = "/login")
public String index(ModelMap modelMap) {
Authentication auth = SecurityContextHolder.getContext()
......@@ -140,4 +150,12 @@ public class BookshareApiController {
private BidEntity convertBidToEntity(BidDto bidDto) {
return modelMapper.map(bidDto, BidEntity.class);
}
private ShareUserDto convertShareUserToDto(ShareUser shareUser) {
return modelMapper.map(shareUser, ShareUserDto.class);
}
private ShareUser convertShareUserToEntity(ShareUserDto shareUserDto) {
return modelMapper.map(shareUserDto, ShareUser.class);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment