public Ad(CreateVM vm) { this.Title = vm.Title; this.Description = vm.Description; this.Price = vm.Price; this.CreationDate = DateTime.Now; this.Location = vm.Location; this.Category = vm.Category; using (var binaryReader = new BinaryReader(vm.PhotoFile.OpenReadStream())) { this.Photo = binaryReader.ReadBytes((int)vm.PhotoFile.Length); } this.Brand = vm.Brand; }
public void Update(CreateVM vm, NotificationRepository notificationRepo, Func <int, string, Task> notify) { TEntity currAd = GetById(vm.Id); if (vm.Title != null && vm.Title.Trim() != "" && vm.Title != currAd.Title) { currAd.Title = vm.Title; } else if (vm.Description != null && vm.Description.Trim() != "" && vm.Description != currAd.Description) { currAd.Description = vm.Description; } else if (vm.Price != 0 && vm.Price != currAd.Price) { currAd.Price = vm.Price; string message = $"<a href='/Home/Ad/?id={vm.Id}&category={vm.Category}'>{currAd.Title}`s</a> new price is {currAd.Price} <span class='notification-date'>[${DateTime.Now}]</span>"; int userId; for (int i = 0; i < currAd.Subscribers.Count; i++) { userId = currAd.Subscribers[i]; notificationRepo.Create(new Notification(message, NotificationType.PriceTrack, userId)); notify(userId, message); } } else if (vm.PhotoFile != null) { using (var binaryReader = new BinaryReader(vm.PhotoFile.OpenReadStream())) { byte[] photoBytes = binaryReader.ReadBytes((int)vm.PhotoFile.Length); if (currAd.Photo != photoBytes) { currAd.Photo = photoBytes; } } } ctx.SaveChanges(); }
public int CreateAd(CreateVM createVM, int userId) { if (createVM.Category == "smartphone") { SmartphoneAd categoryAd = new SmartphoneAd(createVM) { UserId = userId }; smartphoneRepo.Create(categoryAd); return(categoryAd.Id); } else if (createVM.Category == "laptop") { LaptopAd categoryAd = new LaptopAd(createVM) { UserId = userId }; laptopRepo.Create(categoryAd); return(categoryAd.Id); } else if (createVM.Category == "videocard") { VideocardAd categoryAd = new VideocardAd(createVM) { UserId = userId }; videocardRepo.Create(categoryAd); return(categoryAd.Id); } else if (createVM.Category == "monitor") { MonitorAd categoryAd = new MonitorAd(createVM) { UserId = userId }; monitorRepo.Create(categoryAd); return(categoryAd.Id); } else if (createVM.Category == "processor") { ProcessorAd categoryAd = new ProcessorAd(createVM) { UserId = userId }; processorRepo.Create(categoryAd); return(categoryAd.Id); } else if (createVM.Category == "ram") { RAMAd categoryAd = new RAMAd(createVM) { UserId = userId }; ramRepo.Create(categoryAd); return(categoryAd.Id); } else if (createVM.Category == "drive") { DriveAd categoryAd = new DriveAd(createVM) { UserId = userId }; driveRepo.Create(categoryAd); return(categoryAd.Id); } return(0); }