public async Task<ResponseViewModel> UpdateUser(UserDto request)
 {
     ResponseViewModel response = new ResponseViewModel();
     try
     {
         var user = _mapper.Map<User>(request);
         var filter = Builders<User>.Filter.Eq(x => x.Id, user.Id);
         Expression<Func<User, bool>> whereCondition = x => ((!string.IsNullOrEmpty(user.Id) && x.Id == user.Id));
         var model = _userRepository.GetById(whereCondition).FirstOrDefault();
         if (model != null)
         {
             model.FirstName = user.FirstName ?? null;
             model.LastName = user.LastName ?? null;
             model.ModifiedDate = DateTime.UtcNow;
             var result = await _userRepository.Update(filter, model);
             if (result.ModifiedCount == (int)Numbers.One)
             {
                 response.Message = Constants.Success;
                 response.ResponseData = request;
                 response.Status = (int)Numbers.One;
             }
             else if (result.ModifiedCount < (int)Numbers.One)
             {
                 response.Message = Constants.Error;
                 response.Status = (int)Numbers.Zero;
             }
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
     return response;
 }
Example #2
0
        public IActionResult UpsertStudent(Student student)
        {
            var existingStudentId = Request.Form["Id"].ToString();

            if (!string.IsNullOrEmpty(existingStudentId))
            {
                student.Id = ObjectId.Parse(existingStudentId);
                _studentRepository.Update(student.Id, student);
            }
            else
            {
                _studentRepository.Insert(student);
            }

            return(RedirectToAction("Index"));
        }