public async Task <IActionResult> UpdateProfileTarget([FromBody] UpdateProfileTargetCommand model) { FirebaseUser user = HttpContext.GetFirebaseUser(); model.firebaseId = user.UserId; ResultWrapper <UpdateProfileTargetOutput> result = new ResultWrapper <UpdateProfileTargetOutput>(); result = await _mediator.Send(model); return(Ok(result)); }
public async Task <ResultWrapper <UpdateProfileTargetOutput> > Handle(UpdateProfileTargetCommand request, CancellationToken cancellationToken) { ResultWrapper <UpdateProfileTargetOutput> updateProfileResult = new ResultWrapper <UpdateProfileTargetOutput>(); TUser tUser = _dbContext.TUser.FirstOrDefault(x => x.FireBaseId == request.firebaseId); if (tUser == null) { updateProfileResult.Status = false; updateProfileResult.Message = "User doesn't exists"; return(updateProfileResult); } if (tUser.Weight <= request.TargetWeight && request.GoalId == (int)AppEnums.GoalEnum.LOSE_WEIGHT) { updateProfileResult.Status = false; updateProfileResult.Message = "Target weight is invalid!"; return(updateProfileResult); } if (tUser.Weight >= request.TargetWeight && request.GoalId == (int)AppEnums.GoalEnum.GAIN_WEIGHT) { updateProfileResult.Status = false; updateProfileResult.Message = "Target weight is invalid!"; return(updateProfileResult); } if (tUser.Weight != request.TargetWeight && request.GoalId == (int)AppEnums.GoalEnum.MAINTAIN_WEIGHT) { updateProfileResult.Status = false; updateProfileResult.Message = "Target weight is invalid!"; return(updateProfileResult); } if (!_dbContext.TActivityLevel.Any(x => x.Id == request.ActivityLevelId)) { updateProfileResult.Status = false; updateProfileResult.Message = "select your activity level!"; return(updateProfileResult); } if (!_dbContext.TWeeklyGoal.Any(x => x.Id == request.WeeklyGoalId)) { updateProfileResult.Status = false; updateProfileResult.Message = "select your weekly goal!"; return(updateProfileResult); } tUser.TActivityLevelId = request.ActivityLevelId; tUser.TWeeklyGoalId = request.WeeklyGoalId; tUser.TargetWeight = request.TargetWeight; _dbContext.TUser.Update(tUser); int r = await _dbContext.SaveChangesAsync(); if (r <= 0) { updateProfileResult.Status = false; updateProfileResult.Message = "Unhandled error!"; return(updateProfileResult); } updateProfileResult.Status = true; updateProfileResult.Result = new UpdateProfileTargetOutput() { }; return(updateProfileResult); }