public async Task <DiaryGrpcResponse> GetDiariesAsync(DiaryGrpcRequest request, CallContext context = default) { var response = new DiaryGrpcResponse(); try { var diaries = await _diaryFileService.GetDiariesAsync(request.Offset, request.Count, request.Year, request.Month); var diaryRuntimeList = new List <DiaryRuntime>(); foreach (var diary in diaries) { var diaryRuntime = new DiaryRuntime(diary); if (request.ExtractRuntime) { diaryRuntime.ExtractRuntimeData(); } diaryRuntimeList.Add(diaryRuntime); } response.DiaryRuntimeList = diaryRuntimeList; } catch (Exception ex) { response.IsOk = false; response.Message = ex.Message; _logger.LogError(ex, $"Get diaries failed: {JsonUtil.Serialize(request)}"); } return(response); }
public async Task <DiaryGrpcResponse> GetDiaryAsync(DiaryGrpcRequest request, CallContext context = default) { var response = new DiaryGrpcResponse(); try { var diary = await _diaryFileService.GetDiaryAsync(request.Date); if (diary == null) { response.NotFound = true; } else { var diaryRuntime = new DiaryRuntime(diary); if (request.ExtractRuntime) { diaryRuntime.ExtractRuntimeData(); } List <Diary> allDiaries = null; if (request.ExtractPrev) { allDiaries = await _diaryFileService.GetDiariesAsync(); var prevDiary = allDiaries.FirstOrDefault(x => x.Date < request.Date); if (prevDiary != null) { diaryRuntime.Prev = new DiaryRuntime(prevDiary); } } if (request.ExtractNext) { allDiaries ??= await _diaryFileService.GetDiariesAsync(); var nextDiary = allDiaries.LastOrDefault(x => x.Date > request.Date); if (nextDiary != null) { diaryRuntime.Next = new DiaryRuntime(nextDiary); } } response.DiaryRuntime = diaryRuntime; } } catch (Exception ex) { response.IsOk = false; response.Message = ex.Message; _logger.LogError(ex, $"Get diary failed: {JsonUtil.Serialize(request)}"); } return(response); }