Skip to content
This repository has been archived by the owner on Apr 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #43 from WisdomWorks/WW-93
Browse files Browse the repository at this point in the history
WW-93
  • Loading branch information
HoXuanHieu authored Mar 3, 2024
2 parents d30303a + 8e1ad5d commit a4fcd26
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ResponseEntity<?> getAll(){
public ResponseEntity<?> createOne(@RequestBody Course course) {
Course result = courseService.createOne(course);
if(result == null){
return ResponseEntity.status(HttpStatus.CREATED).body("Failed to create ne course");
return ResponseEntity.status(HttpStatus.CREATED).body("Failed to create new course");
}
return ResponseEntity.status(HttpStatus.CREATED).body(result);
}
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/com/example/codeE/controller/GroupController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.example.codeE.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;

import com.example.codeE.model.group.Group;
import com.example.codeE.service.group.GroupService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/group")
@Validated
public class GroupController {
@Autowired
private GroupService groupService;

@GetMapping
@RequestMapping(value = "",method = RequestMethod.GET)
public ResponseEntity<?> getAllGroups() {
return new ResponseEntity<>(groupService.getAll(), HttpStatus.OK);
}

@GetMapping
@RequestMapping(value = "{groupId}",method = RequestMethod.GET)
public ResponseEntity<?> getGroupById(@PathVariable String groupId) {
return new ResponseEntity<>(groupService.getById(groupId), HttpStatus.OK);
}

@PostMapping
@RequestMapping(value = "", method = RequestMethod.POST)
public ResponseEntity<?> createGroup(@RequestBody Group group) {
return new ResponseEntity<>(groupService.createOne(group), HttpStatus.CREATED);
}

// @PutMapping("/{groupId}")
// public ResponseEntity<?> updateGroup(String groupId, @RequestBody Group group) {
// return new ResponseEntity<>(groupService.updateGroupById(groupId, group), HttpStatus.OK);
// }
// @DeleteMapping("/{groupId}")
// public ResponseEntity<?> deleteGroup(String groupId) {
// groupService.deleteById(groupId);
// return new ResponseEntity<>(HttpStatus.OK);
// }
}
45 changes: 35 additions & 10 deletions src/main/java/com/example/codeE/model/group/Group.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,52 @@
package com.example.codeE.model.group;

import java.sql.Date;
import java.time.LocalDateTime;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.PrePersist;
import jakarta.persistence.PreUpdate;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity(name = "group")
public class Group {
@NonNull
@Id
@NotBlank(message = "Group id is required")
@Column(name = "group_id")
private String groupId;
@NonNull

@NotBlank(message = "Course id is required")
@Column(name = "course_id")
private String courseId;
@NonNull

@NotBlank(message = "Group name is required")
@Column(name = "group_name")
private String groupName;
@NonNull
private Date dateCreate;
@NonNull
private String description;
private int numberMember;

@Column(name = "create_date", nullable = false, updatable = false, columnDefinition = "DATETIME DEFAULT CURRENT_TIMESTAMP")
private LocalDateTime dateCreate;

@Column(name = "update_date", nullable = false, columnDefinition = "DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
private LocalDateTime updateDate;

@PrePersist
protected void onCreate() {
LocalDateTime now = LocalDateTime.now();
this.dateCreate = now;
this.updateDate = now;
}

@PreUpdate
protected void onUpdate() {
updateDate = LocalDateTime.now();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.codeE.repository;

import com.example.codeE.model.group.Group;
import org.springframework.data.jpa.repository.JpaRepository;
public interface GroupRepository extends JpaRepository<Group, String>{
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.example.codeE.service.common;

import java.util.List;
import java.util.Map;

public interface CommonService<T> {
T createOne(T entity);
public interface CommonService<T, V> {
T createOne(V entity);
T getById(String id);
List<T> getAll();
//T updateById(String id, Map<String, Object> update);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import com.example.codeE.service.common.CommonService;
import org.springframework.web.multipart.MultipartFile;

public interface CourseService extends CommonService<Course> {
public interface CourseService extends CommonService<Course, Course> {
Course updateById(String id, UpdateCourseRequest update);
boolean importByExcel(MultipartFile file);
Boolean checkCourseExistById(String groupId);
}
68 changes: 68 additions & 0 deletions src/main/java/com/example/codeE/service/group/GroupImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.example.codeE.service.group;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;

import com.example.codeE.model.group.Group;
import com.example.codeE.model.user.User;
import com.example.codeE.repository.GroupRepository;
import org.springframework.stereotype.Service;

@Service
public class GroupImpl implements GroupService{

@Autowired
private GroupRepository groupRepository;

@Override
public Group createOne(Group group) {
group.setGroupId(UUID.randomUUID().toString());
return this.groupRepository.save(group);
}

@Override
public Group getById(String GroupId) {
Optional<Group> groupOptional = this.groupRepository.findById(GroupId);
return groupOptional.orElse(null);
}

@Override
public List<Group> getAll() {
var result = this.groupRepository.findAll();
if(result.size() > 0) {
return result;
}else
return null;
}

@Override
public boolean deleteById(String id) {
if (!groupRepository.existsById(id)) {
return false;
}
this.groupRepository.deleteById(id);
return true;
}

@Override
public List<Group> getGroupsByCourseId(String courseId) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getGroupsByCourseId'");
}

@Override
public List<User> getUsersInGroup(String groupId) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getUsersInGroup'");
}

@Override
public Boolean updateGroupById(String groupId, Group updatedGroup) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'updateGroupById'");
}

}
13 changes: 13 additions & 0 deletions src/main/java/com/example/codeE/service/group/GroupService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.codeE.service.group;

import java.util.List;

import com.example.codeE.model.group.Group;
import com.example.codeE.model.user.User;
import com.example.codeE.service.common.CommonService;

public interface GroupService extends CommonService<Group> {
Boolean updateGroupById(String groupId, Group updatedGroup);
List<Group> getGroupsByCourseId(String courseId);
List<User> getUsersInGroup(String groupId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import java.util.List;

public interface UserService extends CommonService<User> {
public interface UserService extends CommonService<User, User> {
List<User> getUsersByRoleAndSearchKeyword(GetUsersRequest getUsersRequest);
List<User> paginateUsers(GetUsersRequest getUsersRequest);
boolean saveUserToDatabase(MultipartFile file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.example.codeE.repository.UserRepository;
import com.example.codeE.request.course.UpdateCourseRequest;
import com.example.codeE.request.exercise.DeleteExerciseRequest;
import com.example.codeE.request.user.CommonUserRequest;
import com.example.codeE.request.user.UpdateUserRequest;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
Expand Down

0 comments on commit a4fcd26

Please sign in to comment.