Skip to content
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
Empty file modified GPTutor-Backend/mvnw
100644 → 100755
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.chatgpt.controllers;

import com.chatgpt.entity.database.TopicSuggestion;
import com.chatgpt.entity.requests.CreateTopicSuggestion;
import com.chatgpt.entity.requests.UpdateTopicSuggestion;
import com.chatgpt.services.TopicSuggestionService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
public class TopicSuggestionController {

@Autowired
TopicSuggestionService topicSuggestionService;

@PostMapping(path = "/topic-suggestion")
public TopicSuggestion createTopicSuggestion(HttpServletRequest request, @RequestBody CreateTopicSuggestion createTopicSuggestion) {
return topicSuggestionService.createTopicSuggestion((String) request.getAttribute("vkUserId"), createTopicSuggestion);
}

@GetMapping(path = "/topic-suggestion")
public List<TopicSuggestion> getTopicSuggestions(HttpServletRequest request) {
return topicSuggestionService.getTopicSuggestions((String) request.getAttribute("vkUserId"));
}

@GetMapping(path = "/topic-suggestion/all")
public List<TopicSuggestion> getAllTopicSuggestions() {
return topicSuggestionService.getAllTopicSuggestions();
}

@GetMapping(path = "/topic-suggestion/status/{status}")
public List<TopicSuggestion> getTopicSuggestionsByStatus(@PathVariable("status") TopicSuggestion.SuggestionStatus status) {
return topicSuggestionService.getTopicSuggestionsByStatus(status);
}

@GetMapping(path = "/topic-suggestion/type/{type}")
public List<TopicSuggestion> getTopicSuggestionsByType(@PathVariable("type") TopicSuggestion.TopicType type) {
return topicSuggestionService.getTopicSuggestionsByType(type);
}

@DeleteMapping(path = "/topic-suggestion/{id}")
public void deleteTopicSuggestion(HttpServletRequest request, @PathVariable("id") UUID topicSuggestionId) {
topicSuggestionService.deleteTopicSuggestion((String) request.getAttribute("vkUserId"), topicSuggestionId);
}

@PutMapping(path = "/topic-suggestion")
public void updateTopicSuggestion(HttpServletRequest request, @RequestBody UpdateTopicSuggestion updateTopicSuggestion) {
topicSuggestionService.updateTopicSuggestion((String) request.getAttribute("vkUserId"), updateTopicSuggestion);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.chatgpt.entity.database;

import com.chatgpt.entity.VkUser;
import jakarta.persistence.*;

import java.time.LocalDateTime;
import java.util.UUID;

@Entity
public class TopicSuggestion {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
private UUID id;

@ManyToOne()
private VkUser vkUser;

private String title;

private String description;

@Enumerated(EnumType.STRING)
private TopicType type;

@Enumerated(EnumType.STRING)
private SuggestionStatus status;

private LocalDateTime createdAt;

private LocalDateTime updatedAt;

public enum TopicType {
LESSON,
CONVERSATION_STARTER,
ADDITIONAL_REQUEST
}

public enum SuggestionStatus {
PENDING,
APPROVED,
REJECTED
}

public TopicSuggestion() {
this.createdAt = LocalDateTime.now();
this.updatedAt = LocalDateTime.now();
this.status = SuggestionStatus.PENDING;
}

public UUID getId() {
return id;
}

public void setId(UUID id) {
this.id = id;
}

public VkUser getVkUser() {
return vkUser;
}

public void setVkUser(VkUser vkUser) {
this.vkUser = vkUser;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public TopicType getType() {
return type;
}

public void setType(TopicType type) {
this.type = type;
}

public SuggestionStatus getStatus() {
return status;
}

public void setStatus(SuggestionStatus status) {
this.status = status;
this.updatedAt = LocalDateTime.now();
}

public LocalDateTime getCreatedAt() {
return createdAt;
}

public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}

public LocalDateTime getUpdatedAt() {
return updatedAt;
}

public void setUpdatedAt(LocalDateTime updatedAt) {
this.updatedAt = updatedAt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.chatgpt.entity.requests;

import com.chatgpt.entity.database.TopicSuggestion;

public class CreateTopicSuggestion {
private String title;
private String description;
private TopicSuggestion.TopicType type;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public TopicSuggestion.TopicType getType() {
return type;
}

public void setType(TopicSuggestion.TopicType type) {
this.type = type;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.chatgpt.entity.requests;

import com.chatgpt.entity.database.TopicSuggestion;

import java.util.UUID;

public class UpdateTopicSuggestion {
private UUID id;
private String title;
private String description;
private TopicSuggestion.TopicType type;
private TopicSuggestion.SuggestionStatus status;

public UUID getId() {
return id;
}

public void setId(UUID id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public TopicSuggestion.TopicType getType() {
return type;
}

public void setType(TopicSuggestion.TopicType type) {
this.type = type;
}

public TopicSuggestion.SuggestionStatus getStatus() {
return status;
}

public void setStatus(TopicSuggestion.SuggestionStatus status) {
this.status = status;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.chatgpt.repositories;

import com.chatgpt.entity.database.TopicSuggestion;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.CrudRepository;

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

public interface TopicSuggestionRepository extends CrudRepository<TopicSuggestion, UUID> {
List<TopicSuggestion> findAllByVkUserId(UUID vkUserId);
Page<TopicSuggestion> findAllByVkUserId(UUID vkUserId, PageRequest pageable);
List<TopicSuggestion> findAllByStatus(TopicSuggestion.SuggestionStatus status);
List<TopicSuggestion> findAllByType(TopicSuggestion.TopicType type);
List<TopicSuggestion> findAllByVkUserIdAndStatus(UUID vkUserId, TopicSuggestion.SuggestionStatus status);
void deleteAllByVkUserId(UUID vkUserId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.chatgpt.services;

import com.chatgpt.entity.VkUser;
import com.chatgpt.entity.database.TopicSuggestion;
import com.chatgpt.entity.requests.CreateTopicSuggestion;
import com.chatgpt.entity.requests.UpdateTopicSuggestion;
import com.chatgpt.repositories.TopicSuggestionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

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

@Service
public class TopicSuggestionService {
@Autowired
UserService userService;

@Autowired
TopicSuggestionRepository topicSuggestionRepository;

public TopicSuggestion createTopicSuggestion(String vkUserId, CreateTopicSuggestion createTopicSuggestion) {
var user = userService.getOrCreateVkUser(vkUserId);
return saveTopicSuggestion(
user,
createTopicSuggestion.getTitle(),
createTopicSuggestion.getDescription(),
createTopicSuggestion.getType()
);
}

public List<TopicSuggestion> getTopicSuggestions(String vkUserId) {
var user = userService.getOrCreateVkUser(vkUserId);
return topicSuggestionRepository.findAllByVkUserId(user.getId());
}

public List<TopicSuggestion> getAllTopicSuggestions() {
return (List<TopicSuggestion>) topicSuggestionRepository.findAll();
}

public List<TopicSuggestion> getTopicSuggestionsByStatus(TopicSuggestion.SuggestionStatus status) {
return topicSuggestionRepository.findAllByStatus(status);
}

public List<TopicSuggestion> getTopicSuggestionsByType(TopicSuggestion.TopicType type) {
return topicSuggestionRepository.findAllByType(type);
}

public void deleteTopicSuggestion(String vkUserId, UUID topicSuggestionId) {
var user = userService.getOrCreateVkUser(vkUserId);
var foundTopicSuggestion = topicSuggestionRepository.findById(topicSuggestionId);

foundTopicSuggestion.ifPresent(topicSuggestion -> checkAccess(user, topicSuggestion));

topicSuggestionRepository.deleteById(topicSuggestionId);
}

public void updateTopicSuggestion(String vkUserId, UpdateTopicSuggestion updateTopicSuggestion) {
var user = userService.getOrCreateVkUser(vkUserId);
var foundTopicSuggestion = topicSuggestionRepository.findById(updateTopicSuggestion.getId());

foundTopicSuggestion.ifPresent(topicSuggestion -> {
checkAccess(user, topicSuggestion);

if (updateTopicSuggestion.getTitle() != null) {
topicSuggestion.setTitle(updateTopicSuggestion.getTitle());
}
if (updateTopicSuggestion.getDescription() != null) {
topicSuggestion.setDescription(updateTopicSuggestion.getDescription());
}
if (updateTopicSuggestion.getType() != null) {
topicSuggestion.setType(updateTopicSuggestion.getType());
}
if (updateTopicSuggestion.getStatus() != null) {
topicSuggestion.setStatus(updateTopicSuggestion.getStatus());
}

topicSuggestionRepository.save(topicSuggestion);
});
}

private void checkAccess(VkUser user, TopicSuggestion topicSuggestion) {
if (!user.getId().equals(topicSuggestion.getVkUser().getId())) {
throw new ResponseStatusException(HttpStatus.FORBIDDEN);
}
}

private TopicSuggestion saveTopicSuggestion(VkUser user, String title, String description, TopicSuggestion.TopicType type) {
var topicSuggestion = new TopicSuggestion();

topicSuggestion.setVkUser(user);
topicSuggestion.setTitle(title);
topicSuggestion.setDescription(description);
topicSuggestion.setType(type);

topicSuggestionRepository.save(topicSuggestion);

return topicSuggestion;
}
}
Loading