File size: 3,473 Bytes
8edbc20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using AutoMapper;
using Backend_Teamwork.src.Entities;
using Backend_Teamwork.src.Repository;
using Backend_Teamwork.src.Utils;
using static Backend_Teamwork.src.DTO.WorkshopDTO;

namespace Backend_Teamwork.src.Services.workshop
{
    public class WorkshopService : IWorkshopService
    {
        protected readonly WorkshopRepository _workshopRepo;
        protected readonly IMapper _mapper;

        public WorkshopService(WorkshopRepository workshopRepo, IMapper mapper)
        {
            _workshopRepo = workshopRepo;
            _mapper = mapper;
        }

        public async Task<WorkshopReadDTO> CreateOneAsync(
            Guid artistId,
            WorkshopCreateDTO createworkshopDto
        )
        {
            var workshop = _mapper.Map<WorkshopCreateDTO, Workshop>(createworkshopDto);
            workshop.UserId = artistId;
            var workshopCreated = await _workshopRepo.CreateOneAsync(workshop);
            return _mapper.Map<Workshop, WorkshopReadDTO>(workshopCreated);
        }

        public async Task<List<WorkshopReadDTO>> GetAllAsync()
        {
            var workshopList = await _workshopRepo.GetAllAsync();
            if (workshopList == null || !workshopList.Any())
            {
                throw CustomException.NotFound("Workshops not found");
            }
            return _mapper.Map<List<Workshop>, List<WorkshopReadDTO>>(workshopList);
        }

        public async Task<List<WorkshopReadDTO>> GetAllAsync(PaginationOptions paginationOptions)
        {
            // Validate pagination options
            if (paginationOptions.PageSize <= 0)
            {
                throw CustomException.BadRequest("Page Size should be greater than 0.");
            }

            if (paginationOptions.PageNumber < 0)
            {
                throw CustomException.BadRequest("Page Number should be 0 or greater.");
            }
            var workshopList = await _workshopRepo.GetAllAsync(paginationOptions);
            if (workshopList == null || !workshopList.Any())
            {
                throw CustomException.NotFound("Workshops not found");
            }
            return _mapper.Map<List<Workshop>, List<WorkshopReadDTO>>(workshopList);
        }

        public async Task<WorkshopReadDTO> GetByIdAsync(Guid id)
        {
            var foundworkshop = await _workshopRepo.GetByIdAsync(id);
            if (foundworkshop == null)
            {
                throw CustomException.NotFound($"Workshop with ID {id} not found.");
            }
            return _mapper.Map<Workshop, WorkshopReadDTO>(foundworkshop);
        }

        public async Task<bool> DeleteOneAsync(Guid id)
        {
            var foundworkshop = await _workshopRepo.GetByIdAsync(id);
            if (foundworkshop == null)
            {
                throw CustomException.NotFound($"Workshop with ID {id} not found.");
            }
            return await _workshopRepo.DeleteOneAsync(foundworkshop);
            ;
        }

        public async Task<bool> UpdateOneAsync(Guid id, WorkshopUpdateDTO workshopupdateDto)
        {
            var foundworkshop = await _workshopRepo.GetByIdAsync(id);
            if (foundworkshop == null)
            {
                throw CustomException.NotFound($"Workshop with ID {id} not found.");
            }
            _mapper.Map(workshopupdateDto, foundworkshop);
            return await _workshopRepo.UpdateOneAsync(foundworkshop);
        }
    }
}