я пытался реализовать сессии обмена опытом и рейтинги с отзывами. Вот что у меня в итоге получилось. Является ли мое решение достаточным для полного функционирования? Что нужно добавить, что нужно исправить в соответствии с ТЗ? Предоставь также полностью работающую систему рейтинга и отзывов. Можно ли сделать так, чтобы рейтинг отображался вместе с соответствующим ему автором или сессией?
session_controller.go:
package controllers
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/BuzzLyutic/Skill-sharing-web-platform/models"
"github.com/BuzzLyutic/Skill-sharing-web-platform/repositories"
)
// SessionController handles session-related HTTP requests
type SessionController struct {
repo *repositories.SessionRepository
}
// NewSessionController creates a new session controller
func NewSessionController(repo *repositories.SessionRepository) *SessionController {
return &SessionController{repo: repo}
}
// GetAll handles GET /sessions
func (c *SessionController) GetAll(ctx *gin.Context) {
sessions, err := c.repo.GetAll()
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
ctx.JSON(http.StatusOK, sessions)
}
// GetByID handles GET /sessions/:id
func (c *SessionController) GetByID(ctx *gin.Context) {
idStr := ctx.Param("id")
id, err := uuid.Parse(idStr)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid session ID format"})
return
}
session, err := c.repo.GetByID(id)
if err != nil {
ctx.JSON(http.StatusNotFound, gin.H{"error": "Session not found"})
return
}
ctx.JSON(http.StatusOK, session)
}
// Create handles POST /sessions
func (c *SessionController) Create(ctx *gin.Context) {
var req models.SessionRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// For demo purposes - in a real app, get the user ID from auth context
creatorID, err := uuid.Parse(ctx.Query("creator_id"))
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid creator ID"})
return
}
session, err := c.repo.Create(creatorID, req)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
ctx.JSON(http.StatusCreated, session)
}
// Update handles PUT /sessions/:id
func (c *SessionController) Update(ctx *gin.Context) {
idStr := ctx.Param("id")
id, err := uuid.Parse(idStr)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid session ID format"})
return
}
var req models.SessionRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
session, err := c.repo.Update(id, req)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
ctx.JSON(http.StatusOK, session)
}
// Delete handles DELETE /sessions/:id
func (c *SessionController) Delete(ctx *gin.Context) {
idStr := ctx.Param("id")
id, err := uuid.Parse(idStr)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid session ID format"})
return
}
if err := c.repo.Delete(id); err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
ctx.JSON(http.StatusOK, gin.H{"message": "Session deleted successfully"})
}
// GetParticipants handles GET /sessions/:id/participants
func (c *SessionController) GetParticipants(ctx *gin.Context) {
idStr := ctx.Param("id")
id, err := uuid.Parse(idStr)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid session ID format"})
return
}
participants, err := c.repo.GetParticipants(id)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
ctx.JSON(http.StatusOK, participants)
}
session_repository.go:
package repositories
import (
"github.com/BuzzLyutic/Skill-sharing-web-platform/models"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
)
// SessionRepository handles database operations for sessions
type SessionRepository struct {
db *sqlx.DB
}
// NewSessionRepository creates a new session repository
func NewSessionRepository(db *sqlx.DB) *SessionRepository {
return &SessionRepository{db: db}
}
// GetAll retrieves all sessions
func (r *SessionRepository) GetAll() ([]models.Session, error) {
sessions := []models.Session{}
query := SELECT * FROM sessions ORDER BY date_time ASC
err := r.db.Select(&sessions, query)
return sessions, err
}
// GetByID retrieves a session by ID
func (r *SessionRepository) GetByID(id uuid.UUID) (models.Session, error) {
var session models.Session
query := SELECT * FROM sessions WHERE id = $1
err := r.db.Get(&session, query, id)
return session, err
}
// Create creates a new session
func (r *SessionRepository) Create(creatorID uuid.UUID, session models.SessionRequest) (models.Session, error) {
var createdSession models.Session
query := INSERT INTO sessions (title, description, category, date_time, location, max_participants, creator_id) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *
err := r.db.Get(&createdSession, query,
session.Title, session.Description, session.Category,
session.DateTime, session.Location, session.MaxParticipants, creatorID)
return createdSession, err
}
// Update updates an existing session
func (r *SessionRepository) Update(id uuid.UUID, session models.SessionRequest) (models.Session, error) {
var updatedSession models.Session
query := UPDATE sessions SET title = $2, description = $3, category = $4, date_time = $5, location = $6, max_participants = $7 WHERE id = $1 RETURNING *
err := r.db.Get(&updatedSession, query,
id, session.Title, session.Description, session.Category,
session.DateTime, session.Location, session.MaxParticipants)
return updatedSession, err
}
// Delete deletes a session
func (r *SessionRepository) Delete(id uuid.UUID) error {
query := DELETE FROM sessions WHERE id = $1
_, err := r.db.Exec(query, id)
return err
}
// GetParticipants gets all participants of a session
func (r *SessionRepository) GetParticipants(sessionID uuid.UUID) ([]models.User, error) {
users := []models.User{}
query := SELECT u.* FROM users u JOIN session_participants sp ON u.id = sp.user_id WHERE sp.session_id = $1
err := r.db.Select(&users, query, sessionID)
return users, err
}