public async Task <SportResponse> GetAsync(int id) { var existingItem = await _sportRepository.FindByIdAsync(id); if (existingItem == null) { return(new SportResponse(404, "Item id not found", "Id", "Sport id not found.")); } var responseResource = _mapper.Map <Sport, SportResource>(existingItem); return(new SportResponse(responseResource)); }
public async Task <CourtResponse> SaveAsync(CourtSaveResource sportSaveResource) { try { var sport = await _sportRepository.FindByIdAsync(sportSaveResource.SportId); if (sport == null) { return(new CourtResponse(404, "Item id not found", "SportId", "Sport id not found.")); } var item = _mapper.Map <CourtSaveResource, Court>(sportSaveResource); await _courtRepository.AddAsync(item); var responseResource = _mapper.Map <Court, CourtResource>(item); return(new CourtResponse(responseResource)); } catch (Exception ex) { return(new CourtResponse(400, "Unexpected error", "Error", ex.Message)); } }
public async Task <SportResponse> UpdateAsync(int id, Sport category) { var existingSport = await _repository.FindByIdAsync(id); if (existingSport == null) { return(new SportResponse("Sport not found.")); } existingSport.Name = category.Name; try { _repository.Update(existingSport); await _unitOfWork.CompleteAsync(); return(new SportResponse(existingSport)); } catch (System.Exception ex) { // Do some logging stuff return(new SportResponse($"An error occurred when updating the sport: {ex.Message}")); } }
public async Task <CourtAvailableListResponse> Availability(string dateString, int sportId, int memberId, int pageNum = 1, int pageSize = 50) { var sport = await _sportRepository.FindByIdAsync(sportId); if (sport == null) { return(new CourtAvailableListResponse(404, "Item id not found", "SportId", "Sport id not found.")); } var member = await _memberRepository.FindByIdAsync(memberId); if (member == null) { return(new CourtAvailableListResponse(404, "Item id not found", "MemberId", "Member id not found.")); } DateTime dateTime = default; if (!String.IsNullOrWhiteSpace(dateString) && !DateTime.TryParse(dateString, out dateTime)) { return(new CourtAvailableListResponse(400, "Wrong date format", "date", "Date format has to be: yyyy-MM-dd")); } if (dateTime < DateTime.Today) { return(new CourtAvailableListResponse(400, "Bad booking time", "date", "Date is older than today")); } var bookingsOfMember = await _bookingRepository.GetBookingsOfDate(dateTime, memberId); if (bookingsOfMember.Count == 3) { return(new CourtAvailableListResponse(400, "Too many bookings", "MemberId", "A member only is allowed to book 3 courts per day")); } var bookingsOfMemberHour = await _bookingRepository.GetBookingsOfHour(dateTime, memberId); if (bookingsOfMemberHour.Count == 2) { return(new CourtAvailableListResponse(400, "Too many bookings", "MemberId", "A member only is allowed to book 2 courts at the same hour")); } var todayBookings = await _bookingRepository.GetBookingsOfDate(dateTime); var hoursByCourt = todayBookings.GroupBy(b => b.CourtId) .ToDictionary(k => k.Key, v => v.Select(f => f.Reservation.ToString("HH:mm")).ToList()); var allCourts = await _courtRepository.ListAsync(pageNum, pageSize); var resources = _mapper.Map <IEnumerable <Court>, IEnumerable <CourtAvailableResource> >(allCourts); List <String> hours = _bookingRepository.GetHourSlots(); foreach (CourtAvailableResource court in resources) { if (hoursByCourt.ContainsKey(court.Id)) { court.AvailableHours = hours.Except(hoursByCourt[court.Id]).ToList(); } else { court.AvailableHours = hours; } } int totalRecords = await _courtRepository.CountAsync(); var resourceList = new CourtAvailableListResource(resources, pageNum, pageSize, totalRecords); return(new CourtAvailableListResponse(resourceList)); }