public Task <ResponseViewModel <UserViewModel> > UpdateUserDataAsync(UserViewModel userData, LoginViewModel loggedUser)
        {
            return(Task.Factory.StartNew(() =>
            {
                var response = new ResponseViewModel <UserViewModel>();
                var userToUpdate = new User();

                response = _validator.IsValidForUpdate(userData, loggedUser);
                if (!response.Success)
                {
                    return response;
                }
                try
                {
                    //Check if vacation record is deleted meanwhile
                    userToUpdate = _context.Users
                                   .SingleOrDefault(c => c.ID == userData.Id);

                    if (userToUpdate == null)
                    {
                        throw new Exception("The user data was deleted by administrator!");
                    }

                    // throws concurrency exception on save changes if item is already modified by another request
                    _context.Entry(userToUpdate).Property("RowVersion").OriginalValue = userData.RowVersion;

                    //change values for update
                    userToUpdate.FirstName = userData.FirstName;
                    userToUpdate.LastName = userData.LastName;
                    userToUpdate.BirthDate = userData.BirthDate;
                    userToUpdate.UserName = userData.UserName;
                    userToUpdate.OfficeCountryCode = userData.OfficeCountryCode;

                    if (loggedUser.Role == Role.Admin)
                    {
                        userToUpdate.Role = userData.Role;
                    }

                    _context.Update(userToUpdate).State = EntityState.Modified;

                    _context.SaveChanges();
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    string errorMessage;
                    var exceptionEntry = ex.Entries.First();
                    var clientValues = (User)exceptionEntry.Entity;
                    var databaseEntry = exceptionEntry.GetDatabaseValues();

                    errorMessage = "Some data changed meanwhile! Click again for update!";

                    if (databaseEntry == null)
                    {
                        errorMessage = "The user data was deleted by administrator!";
                    }
                    else
                    {
                        var databaseValues = (User)databaseEntry.ToObject();

                        if (databaseValues.FirstName != clientValues.FirstName)
                        {
                            errorMessage = "The FirstName is changed by another user! Click again for update!";
                        }
                        if (databaseValues.LastName != clientValues.LastName)
                        {
                            errorMessage = "The LastName is changed by another user! Click again for update!";
                        }
                        if (databaseValues.BirthDate != clientValues.BirthDate)
                        {
                            errorMessage = "The BirthDate is changed by another user! Click again for update!";
                        }
                        if (databaseValues.UserName != clientValues.UserName)
                        {
                            errorMessage = "The UserName is changed by another user! Click again for update!";
                        }
                        if (databaseValues.Role != clientValues.Role)
                        {
                            errorMessage = "The Role is changed by another user! Click again for update!";
                        }
                        if (databaseValues.OfficeCountryCode != clientValues.OfficeCountryCode)
                        {
                            errorMessage = "The OfficeCountryCode is changed by another user! Click again for update!";
                        }
                    }

                    _context.Entry(userToUpdate).State = EntityState.Detached;

                    response.Success = false;
                    response.ResponseMessages.Add(ApplicationConstants.UPDATE_ERROR + " - " + errorMessage);
                    response.ReturnedObject = GetDataById(userData.Id);
                    return response;
                }
                catch (Exception e)
                {
                    response.Success = false;
                    response.ResponseMessages.Add(ApplicationConstants.UPDATE_ERROR + " - " + e.Message);
                    response.ReturnedObject = GetDataById(userData.Id);
                    return response;
                }
                response.Success = true;
                response.ResponseMessages.Add(ApplicationConstants.UPDATE_SUCCESS);
                response.ReturnedObject = GetDataById(userData.Id);

                return response;
            }));
        }
        public Task <ResponseViewModel <List <VacationDataViewModel> > > UpdateVacationAsync(List <VacationDataViewModel> vacations, LoginViewModel loggedUser)
        {
            return(Task.Factory.StartNew(() =>
            {
                var response = new ResponseViewModel <List <VacationDataViewModel> >();
                var vacationToUpdate = new VacationData();

                response = _validator.IsValidForUpdate(vacations, loggedUser);
                if (!response.Success)
                {
                    return response;
                }
                try
                {
                    foreach (var vacation in vacations)
                    {
                        //Check if data is for insert of update
                        var vacationDB = _context.VacationData
                                         .SingleOrDefault(c => c.VacationDate == vacation.CalendarDate && c.UserID == vacation.UserID);

                        if (vacationDB == null && vacation.Id == default(int))
                        {
                            var dataForInsert = VacationDataMapper(vacation);
                            _context.VacationData.Add(dataForInsert);
                        }
                        else
                        {
                            //Check if vacation record is deleted meanwhile
                            vacationToUpdate = _context.VacationData
                                               .SingleOrDefault(c => c.ID == vacation.Id);

                            if (vacationToUpdate == null)
                            {
                                throw new Exception("The vacation was deleted by another user. Click again for insert!");
                            }

                            // throws concurrency exception on save changes if item is already modified by another request
                            _context.Entry(vacationToUpdate).Property("RowVersion").OriginalValue = vacation.RowVersion;

                            //change values for update
                            vacationToUpdate.VacationTypeID = vacation.VacationTypeID;
                            _context.Update(vacationToUpdate).State = EntityState.Modified;
                        }
                    }

                    _context.SaveChanges();
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    string errorMessage;
                    var exceptionEntry = ex.Entries.First();
                    var clientValues = (VacationData)exceptionEntry.Entity;
                    var databaseEntry = exceptionEntry.GetDatabaseValues();

                    if (databaseEntry == null)
                    {
                        errorMessage = "The vacation was deleted by another user. Click again for insert!";
                    }
                    else
                    {
                        var databaseValues = (VacationData)databaseEntry.ToObject();
                        if (databaseValues.VacationTypeID != clientValues.VacationTypeID)
                        {
                            errorMessage = "The Vacation Type is changed by another user! Click again for update!";
                        }
                        else
                        {
                            errorMessage = "Some data changed meanwhile! Click again for update!";
                        }
                    }

                    _context.Entry(vacationToUpdate).State = EntityState.Detached;

                    response.Success = false;
                    response.ResponseMessages.Add(ApplicationConstants.UPDATE_ERROR + " - " + errorMessage);
                    response.ReturnedObject = GetVacationForUser(vacations.FirstOrDefault().CalendarDate.Year,
                                                                 vacations.FirstOrDefault().CalendarDate.Month,
                                                                 null,
                                                                 vacations.FirstOrDefault().UserID);
                }
                catch (Exception e)
                {
                    response.Success = false;
                    response.ResponseMessages.Add(ApplicationConstants.UPDATE_ERROR + " - " + e.Message);
                    response.ReturnedObject = GetVacationForUser(vacations.FirstOrDefault().CalendarDate.Year,
                                                                 vacations.FirstOrDefault().CalendarDate.Month,
                                                                 null,
                                                                 vacations.FirstOrDefault().UserID);
                    return response;
                }
                response.Success = true;
                response.ResponseMessages.Add(ApplicationConstants.UPDATE_SUCCESS);
                response.ReturnedObject = GetVacationForUser(vacations.FirstOrDefault().CalendarDate.Year,
                                                             vacations.FirstOrDefault().CalendarDate.Month,
                                                             null,
                                                             vacations.FirstOrDefault().UserID);
                return response;
            }));
        }