public Product FormProduct(TProducts TProduct)//Формируем продукт используя таблицу TProduct и связанные с ней таблицы { using (postgresContext context = new postgresContext()) { //Составляем листы для жанров мин. и рек. системных требований List <TGameGenre> gameGenre = context.TGameGenre.Include(u => u.IdGameNavigation).Include(u => u.IdGenreNavigation).Where(u => u.IdGame == TProduct.Id).ToList(); List <TMinGameSysReq> minGameSysReq = context.TMinGameSysReq.Include(u => u.IdSysReqNavigation).Include(u => u.IdGameNavigation).Where(u => u.IdGame == TProduct.Id).ToList(); List <TRecGameSysReq> recGameSysReq = context.TRecGameSysReq.Include(u => u.IdSysReqNavigation).Include(u => u.IdGameNavigation).Where(u => u.IdGame == TProduct.Id).ToList(); Product product = new Product(); product.Id = TProduct.Id; product.Name = TProduct.Name; product.Description = TProduct.Description; product.Publisher = TProduct.IdPublisherNavigation.Name; product.Developer = TProduct.IdDeveloperNavigation.Name; product.ReleaseDate = TProduct.ReleaseDate.Date; product.Rate = TProduct.Rate; product.RetailPrice = TProduct.RetailPrice; product.WholesalePrice = TProduct.WholesalePrice; product.GameGenre = new List <string>(); foreach (TGameGenre genre in gameGenre) { product.GameGenre.Add(genre.IdGenreNavigation.Name); } product.MinGameSysReq = new List <string>(); foreach (TMinGameSysReq req in minGameSysReq) { product.MinGameSysReq.Add(req.IdSysReqNavigation.Name + ": " + req.Description); } product.RecGameSysReq = new List <string>(); foreach (TRecGameSysReq req in recGameSysReq) { product.RecGameSysReq.Add(req.IdSysReqNavigation.Name + ": " + req.Description); } //Получаем основное изображение игры using (FileStream fstream = File.OpenRead($@"{BaseSettings.Default.SourcePath}\Products\{product.Id}\MainImage.encr")) { product.MainImage = new byte[fstream.Length]; fstream.Read(product.MainImage, 0, product.MainImage.Length); } return(product); } }
public void CalculateGameScore(int idGame) { using (postgresContext context = new postgresContext()) { //Находим нужную нам игру TProducts product = context.TProducts.FirstOrDefault(u => u.Id == idGame); //Вычисляем её среднюю оценку product.Rate = context.TComments.Where(u => u.IdProduct == idGame).Average(u => u.Score); //Обновляем её в БД context.TProducts.Update(product); //Сохраняем изменения в БД context.SaveChanges(); } }
/// <summary> /// Метод для добавления продукта в магазин /// </summary> public void AddProduct(int idModerateProduct) { using (postgresContext context = new postgresContext()) { TProducts TProduct = new TProducts(); TModerateProducts moderateProduct = context.TModerateProducts.FirstOrDefault(u => u.Id == idModerateProduct); List <TProducts> products = context.TProducts.ToList(); TProduct.Id = products.Count > 0 ? (products.Max(u => u.Id) + 1) : 1; TProduct.Name = moderateProduct.Name; TProduct.Description = moderateProduct.Description; TProduct.IdDeveloper = moderateProduct.IdDeveloper; TProduct.IdPublisher = moderateProduct.IdPublisher; string pathToModeratateProduct = $@"{BaseSettings.Default.SourcePath}\ModerateProducts\{idModerateProduct}"; string pathToProduct = $@"{BaseSettings.Default.SourcePath}\Products\{TProduct.Id}"; //Создать идентичное дерево каталогов foreach (string dirPath in Directory.GetDirectories(pathToModeratateProduct, "*", SearchOption.AllDirectories)) { Directory.CreateDirectory(dirPath.Replace(pathToModeratateProduct, pathToProduct)); } //Скопировать все файлы. И перезаписать(если такие существуют) foreach (string newPath in Directory.GetFiles(pathToModeratateProduct, "*.*", SearchOption.AllDirectories)) { File.Copy(newPath, newPath.Replace(pathToModeratateProduct, pathToProduct), true); } //Удалить все файлы foreach (string newPath in Directory.GetFiles(pathToModeratateProduct, "*.*", SearchOption.AllDirectories)) { File.Delete(newPath); } TProduct.Quantity = 100; TProduct.ReleaseDate = DateTime.Now.Date; TProduct.RetailPrice = moderateProduct.RetailPrice; TProduct.WholesalePrice = moderateProduct.WholesalePrice; context.TProducts.Add(TProduct); context.SaveChanges(); } }