public IHttpActionResult PostUserPerformance(UserPerformance userPerformance) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.UserPerformances.Add(userPerformance); try { db.SaveChanges(); } catch (DbUpdateException) { if (UserPerformanceExists(userPerformance.UserId)) { return(Conflict()); } else { throw; } } return(CreatedAtRoute("DefaultApi", new { id = userPerformance.UserId }, userPerformance)); }
public async Task <UserPerformance> SubmitUserPerformance(UserPerformance userPerformance) { try { var performance = await Context.UserPerformances.Where(p => p.UserId == userPerformance.UserId).FirstOrDefaultAsync(); if (performance == null) { await Context.UserPerformances.AddAsync(userPerformance); await SaveAll(); return(userPerformance); } performance.PerformanceRating = userPerformance.PerformanceRating; performance.OverTime = userPerformance.OverTime; performance.JobInvolvement = userPerformance.JobInvolvement; performance.TrainingTimesLastYear = userPerformance.TrainingTimesLastYear; // performance.ManagerId = userPerformance.ManagerId; performance.Datetime = userPerformance.Datetime; // performance.LastManagerUpdate = userPerformance.LastManagerUpdate; // performance.LastPromotionUpdate = userPerformance.LastPromotionUpdate; // performance.LastRoleUpdate = userPerformance.LastRoleUpdate; await SaveAll(); return(performance); } catch (Exception) { return(null); } }
public void SetPerformancesUser(long userId, Dictionary <string, int> perfList) { CheckPerfomance(); var user = DataContext.Users.First(x => x.Id == userId); foreach (var item in perfList) { var performance = DataContext.Performances.First(x => x.Name.Equals(item.Key)); var userPerformance = new UserPerformance { Performance = performance, Level = item.Value, User = user }; var usrPerf = DataContext.UserPerformances .Include(x => x.User) .Include(x => x.Performance) .FirstOrDefault(x => x.User.Id == userId && x.Performance.Id == performance.Id); if (usrPerf == null) { DataContext.UserPerformances.Add(userPerformance); } else { usrPerf.Level = item.Value; } } DataContext.SaveChanges(); }
public async Task <IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = new ApplicationUser() { UserName = model.Email, Email = model.Email }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (!result.Succeeded) { return(GetErrorResult(result)); } else { UserPerformancesController userPerformancesController = new UserPerformancesController(); UserPerformance userPerformance = new UserPerformance(); userPerformance.UserId = user.UserName; userPerformance.CorrectAnswerCount = 0; userPerformance.TotalGamesCount = 0; userPerformance.TotalQuestions = 0; userPerformancesController.PostUserPerformance(userPerformance); } return(Ok()); }
public IHttpActionResult GetMyPerformance() { UserPerformance userPerformance = db.UserPerformances.Find(User.Identity.Name); if (userPerformance == null) { return(NotFound()); } return(Ok(userPerformance)); }
public IHttpActionResult GetUserPerformance(string id) { UserPerformance userPerformance = db.UserPerformances.Find(id); if (userPerformance == null) { return(NotFound()); } return(Ok(userPerformance)); }
public async Task <IActionResult> UpdateUserJob(int id, UserJobUpdateDto userJobUpdateDto) { try { var user = await UserRepo.GetUser(id); var performance = await PerformanceRepo.GetPerformance(id); if (performance != null) { if (user.JobLevel != userJobUpdateDto.JobLevel) { performance.LastPromotionUpdate = DateTime.Now; await PerformanceRepo.SubmitUserPerformance(performance); } if (user.JobRole != userJobUpdateDto.JobRole) { performance.LastRoleUpdate = DateTime.Now; await PerformanceRepo.SubmitUserPerformance(performance); } if (user.ManagerId != userJobUpdateDto.ManagerId) { performance.LastManagerUpdate = DateTime.Now; await PerformanceRepo.SubmitUserPerformance(performance); } } else { performance = new UserPerformance { LastManagerUpdate = DateTime.Now, LastRoleUpdate = DateTime.Now, LastPromotionUpdate = DateTime.Now, UserId = id }; await PerformanceRepo.SubmitUserPerformance(performance); } await UserRepo.UpdateUserJob(id, userJobUpdateDto); return(Ok( new { id, userJobUpdateDto } )); } catch (Exception) { return(StatusCode(500, "Could not send feedback. Please try again later!")); } }
public IHttpActionResult DeleteUserPerformance(string id) { UserPerformance userPerformance = db.UserPerformances.Find(id); if (userPerformance == null) { return(NotFound()); } db.UserPerformances.Remove(userPerformance); db.SaveChanges(); return(Ok(userPerformance)); }
public async Task <IActionResult> SubmitPerformance(UserPerformance userPerformance) { try { userPerformance.Datetime = DateTime.Now; var performance = await PerformanceRepo.SubmitUserPerformance(userPerformance); return(Created( performance.Id.ToString(), performance )); } catch (Exception) { return(StatusCode(500, "Could not submit performance info. Please try again later!")); } }
public IHttpActionResult PutMyPerformance(UserPerformance userPerformance) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (User.Identity.Name != userPerformance.UserId) { return(BadRequest()); } UserPerformance currentPerformance = db.UserPerformances.First(x => x.UserId == userPerformance.UserId); if (currentPerformance != null) { userPerformance.CorrectAnswerCount += currentPerformance.CorrectAnswerCount; userPerformance.TotalGamesCount = currentPerformance.TotalGamesCount + 1; userPerformance.TotalQuestions += currentPerformance.TotalQuestions; } //db.Entry(userPerformance).State = EntityState.Modified; try { db.Set <UserPerformance>().AddOrUpdate(userPerformance); db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!UserPerformanceExists(userPerformance.UserId)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
//private string userPerformanceUrl = "http://192.168.1.3:45455/api/UserPerformances/"; public async Task <UserPerformance> GetMyStats(string accessToken) { var client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); var json = await client.GetStringAsync(App.accessUrl + $"users/{Settings.LoggedInUserId}"); UserFromApi userFromApi = JsonConvert.DeserializeObject <UserFromApi>(json); UserPerformance userPerformance = new UserPerformance { UserId = userFromApi.Id.ToString(), CorrectAnswerCount = userFromApi.CorrectAnswerCount, TotalGamesCount = userFromApi.TotalGamesCount, TotalQuestions = userFromApi.TotalQuestions }; return(userPerformance); }
/// <summary> /// Метод добавления новой характеристики пользователю /// </summary> /// <param name="login"></param> /// <param name="perfName"></param> /// <param name="level"></param> /// <returns></returns> public UserPerformance AddPerformanceToUser(string login, string perfName, int level = 1) { var user = DataContext.Users .FirstOrDefault( x => x.Login.Equals(login, StringComparison.CurrentCultureIgnoreCase)); if (user == null) { return(null); //Пользователь {login} не найден! } var performance = DataContext.Performances .FirstOrDefault( x => x.Name.Equals(perfName, StringComparison.CurrentCultureIgnoreCase)); if (performance == null) { return(null); //Характеристика {perfName} не найдена! } if (DataContext.UserPerformances .FirstOrDefault( x => x.User.Login.Equals(login, StringComparison.CurrentCultureIgnoreCase) && x.Performance.Name.Equals(perfName, StringComparison.CurrentCultureIgnoreCase)) != null) { return(null); //Характеристика {perfName} уже добавлена пользователю {login} ! } var userPerformance = new UserPerformance { User = user, Performance = performance, Level = level }; DataContext.UserPerformances.Add(userPerformance); DataContext.SaveChanges(); return(userPerformance); }
public async Task <bool> SaveUserPerformance(string accessToken, string userName, UserPerformance userPerformance) { var client = new HttpClient(); var json = JsonConvert.SerializeObject(userPerformance); HttpContent httpContent = new StringContent(json); httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); var result = await client.PutAsync(userPerformanceUrl + "PutMyPerformance", httpContent); return(result.IsSuccessStatusCode); }