public async Task <Response <string> > UpdateDiaryRequest(UpdateDiaryRequest request)
        {
            var child = await _unitOfWork.ChildInfoRepository.FirstAsync(x => x.Id == request.ChildId && x.DelFlag == false);

            if (child == null)
            {
                return(new Response <String>(null, $"Không tìm thấy bé \'{request.ChildId}\'."));
            }

            var diary = await _unitOfWork.DiaryRepository.FirstAsync(x => x.Id == request.Id && x.DelFlag == false);

            if (diary == null)
            {
                return(new Response <string>(null, $"Không tìm thấy thông tin nhật ký có id \'{request.Id}\'."));
            }

            diary.ImageUrl         = request.ImageURL;
            diary.DiaryContent     = request.DiaryContent;
            diary.LastModifiedBy   = request.LastModifiedBy;
            diary.LastModifiedTime = DateTimeOffset.Now.ToOffset(new TimeSpan(7, 0, 0)).DateTime;
            diary.PublicDate       = request.PublicDate;
            diary.PublicFlag       = request.PublicFlag;
            diary.ApprovedFlag     = request.ApprovedFlag;

            _unitOfWork.DiaryRepository.UpdateAsync(diary);
            await _unitOfWork.SaveAsync();

            return(new Response <string>(diary.Id.ToString(), $"Cập nhật thông tin nhật ký thành công, id: {diary.Id}"));
        }
        public async Task <IActionResult> UpdateDiary(int Id, UpdateDiaryRequest request)
        {
            if (Id != request.Id)
            {
                return(BadRequest());
            }

            return(Ok(await _diaryService.UpdateDiaryRequest(request)));
        }
示例#3
0
        //public ServiceResponse<IEnumerable<DiaryResponse>> Read()
        //{
        //    List<Diary> diaries = new List<Diary>();
        //    using (DiaryContext db = new DiaryContext())
        //    {
        //        diaries = db.Diaries.Include(x => x.Days).Include(x => x.User).ToList();
        //    }
        //    List<DiaryResponse> diaryResponses = new List<DiaryResponse>();
        //    foreach (var item in diaries)
        //    {
        //        diaryResponses.Add(new DiaryResponse(item));
        //    }
        //    return new ServiceResponse<IEnumerable<DiaryResponse>>(diaryResponses, HttpStatusCode.OK, "Table downloaded!");
        //}

        //public ServiceResponse<DiaryResponse> ReadById(int id)
        //{
        //    DiaryResponse diaryResponse = GetDiary(id);
        //    if(diaryResponse == null)
        //    {
        //        return new ServiceResponse<DiaryResponse>(null, HttpStatusCode.NotFound, "There is not existing diary with given id!");
        //    }
        //    return new ServiceResponse<DiaryResponse>(diaryResponse, HttpStatusCode.OK, "Diary downloaded!");
        //}

        public ServiceResponse <DiaryResponse> Update(UpdateDiaryRequest updateDiaryRequest)
        {
            Diary diaryToUpdate;

            using (DiaryContext db = new DiaryContext())
            {
                if (!db.Diaries.Any(x => x.Id == updateDiaryRequest.Id))
                {
                    return(new ServiceResponse <DiaryResponse>(null, HttpStatusCode.NotFound, "There is not existing diary with given id!"));
                }
                diaryToUpdate = db.Diaries.FirstOrDefault(x => x.Id == updateDiaryRequest.Id);
                if (!string.IsNullOrEmpty(updateDiaryRequest.Conclusions))
                {
                    diaryToUpdate.Conclusions = updateDiaryRequest.Conclusions;
                }
                if (updateDiaryRequest.EndDate.Date.Year > 2019)
                {
                    diaryToUpdate.EndDate = updateDiaryRequest.EndDate;
                }
                if (updateDiaryRequest.StartDate.Date.Year > 2019)
                {
                    diaryToUpdate.StartDate = updateDiaryRequest.StartDate;
                }
                if (updateDiaryRequest.BenchPressStart != 0)
                {
                    diaryToUpdate.BenchPressStart = updateDiaryRequest.BenchPressStart;
                }
                if (updateDiaryRequest.SquatStart != 0)
                {
                    diaryToUpdate.SquatStart = updateDiaryRequest.SquatStart;
                }
                if (updateDiaryRequest.DeadliftStart != 0)
                {
                    diaryToUpdate.DeadliftStart = updateDiaryRequest.DeadliftStart;
                }
                if (updateDiaryRequest.BenchPressEnd != 0)
                {
                    diaryToUpdate.BenchPressEnd = updateDiaryRequest.BenchPressEnd;
                }
                if (updateDiaryRequest.SquatEnd != 0)
                {
                    diaryToUpdate.SquatEnd = updateDiaryRequest.SquatEnd;
                }
                if (updateDiaryRequest.DeadliftEnd != 0)
                {
                    diaryToUpdate.DeadliftEnd = updateDiaryRequest.DeadliftEnd;
                }
                if (updateDiaryRequest.Progress != 0)
                {
                    diaryToUpdate.Progress = updateDiaryRequest.Progress;
                }

                db.SaveChanges();
                return(new ServiceResponse <DiaryResponse>(new DiaryResponse(diaryToUpdate), HttpStatusCode.OK, "User was updated successfully"));
            }
        }
示例#4
0
        public IHttpActionResult Update(UpdateDiaryRequest diary)
        {
            if (diary.Id <= 0)
            {
                return(ResponseMessage(CreateCustomResponseMessage(HttpStatusCode.BadRequest)));
            }
            var result = _diaryService.Update(diary);

            return(ResponseMessage(CreateCustomResponseMessage(result)));
        }