private static RustServerViewModel CopyToViewModel(this RustServer server) { var model = new RustServerViewModel { Id = server.Id.ToString(), Name = server.Name, NameInShop = server.NameInShop, Index = server.Index, IpAddress = server.IpAddress, Port = server.Port, MapName = server.ServerMap.Type, ShowInShop = server.ShowInShop }; return(model); }
public async Task <(RustEditServerResult, string)> UpdateAsync(RustShopViewModel model) { var shop = _context.Shops.FirstOrDefault(x => x.Id == Guid.Parse(model.Id)); var userForLog = await _userManager.FindByEmailAsync(_httpContextAccessor.HttpContext.User.Identity.Name); if (shop is null) { return(RustEditServerResult.NotFound, string.Empty); } if (model.RustServerEditViewModel.Id is null) { var newServerId = Guid.NewGuid(); var newServer = new RustServer { Id = newServerId, Name = model.RustServerEditViewModel.Name, NameInShop = model.RustServerEditViewModel.NameInShop, Index = model.RustServerEditViewModel.Index, IpAddress = model.RustServerEditViewModel.IpAddress, Port = model.RustServerEditViewModel.Port, ShowInShop = model.RustServerEditViewModel.ShowInShop, Shop = shop, ServerMap = _context.RustServerMaps.First(x => x.Id == Guid.Parse(model.RustServerEditViewModel.MapId)) }; _context.RustServers.Add(newServer); await _context.SaveChangesAsync(); _logger.LogInformation("UserName: {0} | UserId: {1} | Request: {2} | PostMessage: {3}", userForLog.UserName, userForLog.Id, _httpContextAccessor.HttpContext.Request.GetRawTarget(), $"Server was successfully created. ServerId: {newServer.Id}"); return(RustEditServerResult.Created, newServerId.ToString()); } var server = _context.RustServers.FirstOrDefault(x => x.Id == Guid.Parse(model.RustServerEditViewModel.Id)); if (server is null) { return(RustEditServerResult.NotFound, String.Empty); } server.Name = model.RustServerEditViewModel.Name; server.NameInShop = model.RustServerEditViewModel.NameInShop; server.Index = model.RustServerEditViewModel.Index; server.IpAddress = model.RustServerEditViewModel.IpAddress; server.Port = model.RustServerEditViewModel.Port; server.ShowInShop = model.RustServerEditViewModel.ShowInShop; server.Shop = shop; if (model.RustServerEditViewModel.MapId != null) { var map = _context.RustServerMaps.FirstOrDefault(x => x.Id == Guid.Parse(model.RustServerEditViewModel.MapId)); server.ServerMap = map; } _context.RustServers.Update(server); await _context.SaveChangesAsync(); _logger.LogInformation("UserName: {0} | UserId: {1} | Request: {2} | PostMessage: {3}", userForLog.UserName, userForLog.Id, _httpContextAccessor.HttpContext.Request.GetRawTarget(), $"Server was successfully updated. ServerId: {server.Id}"); return(RustEditServerResult.Updated, server.Id.ToString()); }
public static RustServerViewModel CreateRustServerViewModel(this RustServer server) { var model = server.CopyToViewModel(); return(model); }