// GET: UserVersions/Create //calls the second version create using parameters passed to it. public async Task <IActionResult> Create(int?id) { var versionId = Convert.ToInt32(id); var userVersion = new UserVersion(); return(await Create(userVersion, versionId)); }
public async Task <IActionResult> Edit(int id, [Bind("UserVersionId,UserId,VersionId")] UserVersion userVersion) { if (id != userVersion.UserVersionId) { return(NotFound()); } ModelState.Remove("UserId"); ModelState.Remove("User"); if (ModelState.IsValid) { try { if (userVersion.Hidden == true) { userVersion.Hidden = false; } else { userVersion.Hidden = true; } _context.Update(userVersion); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserVersionExists(userVersion.UserVersionId)) { return(NotFound()); } else { throw; } } //get the pluginId for navigation back to plugin page. var PluginId = _context.Versions.FirstOrDefault(v => v.VersionId == userVersion.VersionId).PluginId; //navigate back to plugin page. return(RedirectToAction("Details", "Plugins", new { id = PluginId })); } return(NotFound()); }
public async Task <IActionResult> Create([Bind("UserVersionId,UserId,VersionId")] UserVersion userVersion, int id) { ModelState.Remove("UserId"); ModelState.Remove("User"); //create UserVersion, setting its two parameters without user input and returning to plugin page. if (ModelState.IsValid) { var currentUser = await GetCurrentUserAsync(); userVersion.VersionId = id; userVersion.UserId = currentUser.Id; _context.Add(userVersion); await _context.SaveChangesAsync(); //after saving, find plugin that matches the version Id, and store its Id (for navigation back to plugin page). var PluginId = _context.Versions.FirstOrDefault(v => v.VersionId == id).PluginId; //navigate back to plugin page. return(RedirectToAction("Details", "Plugins", new { id = PluginId })); } return(NotFound()); }
/// <summary> /// 登録済みの最新のユーザマスタバージョンを検索し、存在する場合は有効期限日付時刻を現在時刻で更新する。ユーザマスタバージョンを追加してバージョン番号を取得する。 /// </summary> public long Add() { UserVersion uv = new UserVersion(); uv.ExpirationDate = DateTime.MaxValue; var now = DateTime.Now; uv.RegisterDate = now; long latestVersion = GetLatestVersion(); if (latestVersion > 0) { UserVersion latestUserVer = _context.UserVersions.Find(GetLatestVersion()); latestUserVer.ExpirationDate = now; _context.UserVersions.Update(latestUserVer); } _context.UserVersions.Add(uv); _context.SaveChanges(); return(GetLatestVersion()); }
/// <summary> /// 担当者情報をアップロードして、一括登録 /// </summary> /// <param name="userList">一括登録のユーザリスト</param> /// <returns></returns> public async Task UploadAsync(List <UserMaster> userList) { _logger.LogTrace(DateTime.Now + "|DBへ担当者一括登録処理開始"); using (var transaction = _context.Database.BeginTransaction()) { try { var uv = new UserVersion(); uv.ExpirationDate = DateTime.MaxValue; var now = DateTime.Now; uv.RegisterDate = now; if (_context.UserVersions.Any()) { var latestWsv = _context.UserVersions.OrderByDescending(e => e.Id).First(); latestWsv.ExpirationDate = now; } _context.UserVersions.Add(uv); _logger.LogTrace(DateTime.Now + $"|ユーザバージョン更新済。"); var changedUserlist = new List <UserMaster>(); userList.ForEach(user => { //DBに新規作成又は更新前に、部分コラムにデフォルト値を設定 user.Role = ApiConstant.ROLE_WORK; user.IsValid = true; user.Password = CreatePasswordHash(user.Password); user.RemoveDate = DateTime.MaxValue; UserMaster dbUser = GetById(user.LoginId); if (null == dbUser)//存在しなければ新規作成 { _context.UserMasters.Add(user); dbUser = user; } else//既存のデータを更新 { dbUser.LoginName = user.LoginName; dbUser.Role = user.Role; dbUser.IsValid = user.IsValid; dbUser.Password = user.Password; dbUser.RemoveDate = user.RemoveDate; } changedUserlist.Add(dbUser); }); _logger.LogTrace(DateTime.Now + $"|ユーザマスタにADD又はUPDATEで更新済:合計{userList.Count}件"); //全て有効的な普通のユーザを取得 var allUsers = _context.UserMasters.Where(userdb => userdb.IsValid && userdb.Role == ApiConstant.ROLE_WORK && userdb.RemoveDate == DateTime.MaxValue); //今回一括登録されたユーザリストに含まれていないユーザを取得 var expiredUserList = allUsers.Except(changedUserlist); //論理削除 foreach (var expiredUser in expiredUserList) { expiredUser.IsValid = false; expiredUser.RemoveDate = now; } ; _logger.LogTrace(DateTime.Now + $"|ユーザマスタに論理削除の更新済:合計{expiredUserList.Count()}件"); //非同期で保存 await _context.SaveChangesAsync(); transaction.Commit(); _logger.LogTrace(DateTime.Now + $"|担当者一括登録済:合計{userList.Count}件"); } catch (Exception e) { transaction.Rollback(); _logger.LogError(DateTime.Now + "|処理失敗、ロールバック済。Exceptionメッセージ:" + e.Message); throw; } } }
// GET: Plugins/Details/5 public async Task <IActionResult> Details(int?id) { var currentUser = await GetCurrentUserAsync(); //get query string value i.e.: "?version=1" var query = Request.QueryString.ToString(); if (id == null) { return(NotFound()); } ModelState.Remove("UserId"); ModelState.Remove("User"); var plugin = await _context.Plugins .Include(p => p.Engine) .Include(p => p.PluginType) .Include(p => p.User) .FirstOrDefaultAsync(m => m.PluginId == id); //get all associated versions, ordered in descending order by their iteration #s. var versions = _context.Versions.Where(v => v.PluginId == id).OrderByDescending(v => v.Iteration); if (plugin == null) { return(NotFound()); } //add webpage url to viewbag (using @model.displayfor as the url causes bugs) if (plugin.Webpage != null) { ViewBag.Url = plugin.Webpage; } else { ViewBag.Url = ""; } //strongly typed local variable to be set in the following if/else statement. Version currentVersion; //set the current display version by the url string, or set it to the latest by default if none exists. if (query.Contains("version")) { //get version # as string from url query string var versionString = HttpUtility.ParseQueryString(query).Get("version"); //convert string to int var versionNum = Convert.ToInt32(versionString); //compare into to version ids of all versions and get the one that matches, set it as the currentVersion. currentVersion = versions.FirstOrDefault(v => v.VersionId == versionNum); } else { //if no version Id can be derrived from the url query, select the first (latest iteration). currentVersion = versions.FirstOrDefault(); } if (currentVersion != null && currentVersion.DownloadLink != null) { ViewBag.Vurl = currentVersion.DownloadLink; } else { ViewBag.Vurl = ""; } //get user version if one exists, otherwise it can be set to null. UserVersion userVersion = null; //start by finding all that match userId, then including their version. var userVersions = _context.UserVersions.Where(uv => uv.UserId == currentUser.Id).Include(uv => uv.Version); //get the pluginId from the int? that was passed to this method. var pluginId = Convert.ToInt32(id); //filter userVersions by the pluginId where it matches the version's plugin Id. IQueryable <UserVersion> matchingUserVersions = null; if (currentUser != null && userVersions != null) { matchingUserVersions = userVersions.Where(uv => uv.Version.PluginId == pluginId); if (matchingUserVersions != null) { if (matchingUserVersions.Count() > 1) { //always get the earliest (original) user version. userVersion = matchingUserVersions.OrderBy(uv => uv.VersionId).FirstOrDefault(); } else if (matchingUserVersions.Count() == 1) { userVersion = matchingUserVersions.FirstOrDefault(); } } } //instantiate view model and add the version list, current version and plugin to it. string currentUserId; if (currentUser != null) { currentUserId = currentUser.Id; } else { currentUserId = "0"; } var modelView = new PluginVersionModelView() { Versions = versions.ToList(), Plugin = plugin, CurrentVersion = currentVersion, UserVersion = userVersion, currentUserId = currentUserId }; //return the view model. return(View(modelView)); }