public async Task <UrgencyResponse> SaveAsync(int guardianId, Urgency urgency) { var existingGuardian = await _guardianRepository.FindByIdAsync(guardianId); if (existingGuardian == null) { return(new UrgencyResponse("Guardian not found")); } var othersUrgencies = await _urgencyRepository.ListByGuardianIdAsync(guardianId); var otherUrgency = othersUrgencies.Where(other => urgency.Title == other.Title && urgency.Latitude == other.Latitude && urgency.Longitude == other.Longitude && DateTime.Now.Date == other.ReportedAt.Date).ToList().FirstOrDefault(); if (otherUrgency != null) { return(new UrgencyResponse("There is another urgency with same title, location and report day")); } urgency.Guardian = existingGuardian; try { await _urgencyRepository.AddAsync(urgency); await _unitOfWork.CompleteAsync(); return(new UrgencyResponse(urgency)); } catch (Exception ex) { return(new UrgencyResponse($"An error ocurred while saving the urgency: {ex.Message}")); } }
public async Task <GuardianResponse> DeleteAsync(int guardianId) { var existingGuardian = await _guardianRepository.FindByIdAsync(guardianId); if (existingGuardian == null) { return(new GuardianResponse("Guardian not found")); } try { _guardianRepository.Remove(existingGuardian); await _unitOfWork.CompleteAsync(); return(new GuardianResponse(existingGuardian)); } catch (Exception ex) { return(new GuardianResponse($"An error ocurred while deleting the guardian: {ex.Message}")); } }