Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Servisler Düzenlendi, jwt düzenlendi, yeni fonksiyonlar eklendi #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</scm>
<properties>
<java.version>17</java.version>
<jwt.version>0.12.5</jwt.version>
<jwt.version>0.11.5</jwt.version>
</properties>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ public interface EventService {

Result deleteEvent(int id);

// Result addUserToEvent(int eventId, int userId);

//DataResult<List<getEventsDto>> getAllEventsWithDto();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,28 @@
import com.example.gecekodubackend.core.utilities.results.DataResult;
import com.example.gecekodubackend.core.utilities.results.Result;
import com.example.gecekodubackend.core.entities.*;
import org.springframework.security.core.userdetails.UserDetailsService;

import java.util.List;

public interface UserService {
public interface UserService extends UserDetailsService {
DataResult<List<GetUserDto>> getAllUsers();

DataResult<GetUserDto> getUserById(int id);

Result addUser(User user);

Result updateUser(GetUserDto userDto, int id);

Result deleteUser(int id);
DataResult<User> getUserByEmail(String email);

DataResult<GetUserDto> getUserByEmail(String email);

DataResult<User> getUserEntityByEmail(String email);

DataResult<User> getUserEntityById(int id);

Result addUserToEvent(int userId, int eventId);


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@

public interface WorkshopService {
DataResult<List<Workshop>> getAllWorkshops();

DataResult<Workshop> getWorkshopById(int id);

Result addWorkshop(Workshop workshop);

Result updateWorkshop(Workshop workshop,int id);

Result deleteWorkshop(int id);


}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.gecekodubackend.business.concretes;

import com.example.gecekodubackend.business.abstracts.EventService;
import com.example.gecekodubackend.business.abstracts.UserService;
import com.example.gecekodubackend.business.constants.EventMessages;
import com.example.gecekodubackend.business.constants.UserMessages;
import com.example.gecekodubackend.core.utilities.results.*;
import com.example.gecekodubackend.dataAccess.abstracts.EventDao;
import com.example.gecekodubackend.entity.concretes.Event;
Expand All @@ -14,12 +16,15 @@
@Service
public class EventManager implements EventService {

private final EventDao eventDao;
private final EventDao eventDao;


@Autowired
public EventManager(EventDao eventDao) {
super();
this.eventDao = eventDao;


}

/*
Expand Down Expand Up @@ -92,6 +97,8 @@ public Result deleteEvent(int id) {
return new SuccessResult(EventMessages.eventDeletedSuccessfully);
}



public Result checkIfEventExists(int id){
Optional<Event> result = eventDao.findById(id);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.gecekodubackend.business.concretes;

import com.example.gecekodubackend.business.abstracts.EventService;
import com.example.gecekodubackend.business.abstracts.UserService;
import com.example.gecekodubackend.business.constants.UserMessages;
import com.example.gecekodubackend.core.dtos.GetUserDto;
Expand All @@ -8,6 +9,9 @@
import com.example.gecekodubackend.core.entities.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
Expand All @@ -16,29 +20,36 @@
@Service
public class UserManager implements UserService {


private final UserDao userDao;
private EventService eventService;


private BCryptPasswordEncoder passwordEncoder;

@Autowired
public UserManager(UserDao userDao){
public UserManager(UserDao userDao, BCryptPasswordEncoder passwordEncoder, EventService eventService) {
super();
this.userDao = userDao;
this.passwordEncoder = passwordEncoder;
this.eventService = eventService;
}

@Override
public DataResult<List<GetUserDto>> getAllUsers() {
List<GetUserDto> userDtoList = new ArrayList<>();
List<User> users = userDao.findAll();

if (users.isEmpty()){
if(users.isEmpty()){
return new ErrorDataResult<>(UserMessages.usersNotFound);
}

// against to solid? needs some refactor
for(User user : userDtoList){
List<GetUserDto> userDtoList = new ArrayList<>();
for(User user : users){
GetUserDto userDto = new GetUserDto();
BeanUtils.copyProperties(user, userDto);
userDtoList.add(userDto);
}

return new SuccessDataResult<>(userDtoList, UserMessages.usersBroughtSuccessfully);
}

Expand All @@ -50,7 +61,9 @@ public DataResult<GetUserDto> getUserById(int id) {
return new ErrorDataResult<>(UserMessages.userNotFound);
}

return new SuccessDataResult<>(result.get(),UserMessages.userBroughtSuccessfully);
GetUserDto userDto = new GetUserDto();
BeanUtils.copyProperties(result.get(), userDto);
return new SuccessDataResult<>(userDto, UserMessages.userBroughtSuccessfully);
}

@Override
Expand All @@ -66,8 +79,50 @@ public Result deleteUser(int id) {
}

@Override
public DataResult<User> getUserByEmail(String email) {
return null;
public DataResult<GetUserDto> getUserByEmail(String email) {
var result = userDao.getUserByEmail(email);

if(result == null){
return new ErrorDataResult<>(UserMessages.userNotFound);
}

GetUserDto userDto = new GetUserDto();
BeanUtils.copyProperties(result, userDto);
return new SuccessDataResult<>(userDto, UserMessages.userBroughtSuccessfully);
}

@Override
public DataResult<User> getUserEntityByEmail(String email) {
var result = userDao.getUserByEmail(email);

if(result == null){
return new ErrorDataResult<>(UserMessages.userNotFound);
}

return new SuccessDataResult<>(result, UserMessages.userBroughtSuccessfully);
}

@Override
public DataResult<User> getUserEntityById(int id) {
var result = userDao.findById(id);

if(result.isEmpty()){
return new ErrorDataResult<>(UserMessages.userNotFound);
}

return new SuccessDataResult<>(result.get(), UserMessages.userBroughtSuccessfully);
}

@Override
public Result addUserToEvent(int userId, int eventId) {

var userResult = getUserEntityById(userId);
var eventResult = eventService.getEventById(eventId);

userResult.getData().getEvents().add(eventResult.getData());
userDao.save(userResult.getData());

return new SuccessResult(UserMessages.userAddedToEventSuccessfully);
}

@Override
Expand All @@ -78,17 +133,27 @@ public Result updateUser(GetUserDto userDto, int id) {
return new ErrorResult(UserMessages.userNotFound);
}

DataResult<GetUserDto> userToUpdate = getUserById(id);
userToUpdate.getData().setFirstName(userDto.getFirstName());
userToUpdate.getData().setLastName(userDto.getLastName());
userToUpdate.getData().setEmail(userDto.getEmail());
var userToUpdate = userDao.findById(id).get();
userToUpdate.setFirstName(userDto.getFirstName());
userToUpdate.setLastName(userDto.getLastName());
userToUpdate.setEmail(userDto.getEmail());

userDao.save(userToUpdate);
return new SuccessResult(UserMessages.userUpdatedSuccessfully);
}

@Override
public Result addUser(User user) {
return null;

user.setPassword(passwordEncoder.encode(user.getPassword()));

if(userDao.existsByEmail(user.getEmail())){
return new ErrorResult(UserMessages.emailAlreadyExists);
}


userDao.save(user);
return new SuccessResult(UserMessages.userAddedSuccessfully);
}

public Result checkIfUserExists(int id){
Expand All @@ -100,4 +165,10 @@ public Result checkIfUserExists(int id){

return new ErrorResult(UserMessages.userNotFound);
}
@Override

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
var user = getUserEntityByEmail(username).getData();
return user;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public class EventMessages {
public static String eventsAlreadyExists = "Bu etkinlik mevcut!";
public static String eventUpdatedSuccessfully = "Etkinlik başarıyla güncellendi!";
public static String eventDeletedSuccessfully = "Etkinlik başarıyla silindi!";
public static String userAddedToEventSuccessfully = "Kullanıcı etkinliğe başarıyla eklendi!";
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public class UserMessages {
public static String userDeletedSuccessfully = "Kullanıcı başarıyla silindi!";
public static String emailCannotBeNull = "Email adresiniz boş olamaz!";
public static String userDoesNotExist = "Kullanıcı mevcut değil!";
public static String userAddedToEventSuccessfully = "Kullanıcı etkinliğe başarıyla eklendi!";
;
}
Loading