Пример #1
0
 public static string Bet(Person p, int lotId, int money)
 {
     try
     {
         using (AuctionContent db = new AuctionContent())
         {
             db.Persons.Attach(p);
             if (db.Lots.FirstOrDefault(elem => elem.Id == lotId) == null)
             {
                 return("Wrong id of lot");
             }
             if (db.Lots.FirstOrDefault(elem => elem.Id == lotId).TimeFinish < DateTime.Now)
             {
                 return("Lot is finished");
             }
             if (db.Lots.FirstOrDefault(elem => elem.Id == lotId).TimeStart > DateTime.Now)
             {
                 return("Lot is not start");
             }
             if (LastBet(lotId) != 0 && LastBet(lotId) >= money)
             {
                 return("Small bet");
             }
             LotHistory l = new LotHistory()
             {
                 Persson = p, Money = money, Lot = db.Lots.FirstOrDefault(elem => elem.Id == lotId)
             };
             db.History.Add(l);
             db.Lots.First(elem => elem.Id == lotId).History.Add(l);
             db.SaveChanges();
             return("You make Bet!)");
         }
     }
     catch (Exception ex)
     {
         Log.Logger(ex.Message);
         return("Something wrong with db");
     }
 }
Пример #2
0
        public async Task <IActionResult> AddDocuments(int lotId, DocumentsVM vm)
        {
            var identityUser = await _userManager.GetUserAsync(HttpContext.User);

            var loggedInUser = _context.Owner.Find(identityUser.OwnerId);

            var lot = await _context.Lot.FindAsync(lotId);

            var uploadedFiles = HttpContext.Request.Form.Files;

            if (uploadedFiles.Count == 0)
            {
                ModelState.AddModelError("Files", "Please upload at least one file");
            }

            // Assume that every row has a file
            // There is not a method currently to make sure a name/description matches a file, and vice versa
            if (uploadedFiles.Count != vm.Files.Count)
            {
                ModelState.AddModelError("Files", "Please make sure every row has an uploaded file");
            }

            var submittedRows = new List <SunridgeHOA.Models.File>();

            foreach (var file in vm.Files)
            {
                var hasName = !String.IsNullOrEmpty(file.Name);
                var hasDesc = !String.IsNullOrEmpty(file.Description);

                // This was a blank row, can ignore
                if (!hasName && !hasDesc)
                {
                    continue;
                }

                if (String.IsNullOrEmpty(file.Name) || String.IsNullOrEmpty(file.Description))
                {
                    ModelState.AddModelError("Files", "Please make sure every file has a name and description");
                    break;
                }

                // Add the file row to the array, so we can safely ignore empty rows
                submittedRows.Add(file);
            }

            if (ModelState.IsValid)
            {
                //foreach (var file in submittedRows)
                for (var i = 0; i < submittedRows.Count; i++)
                {
                    var fileDesc = submittedRows[i];
                    var file     = uploadedFiles[i];

                    var webRootPath = _hostingEnv.WebRootPath;
                    var folder      = SD.LotDocsFolder;
                    var uploads     = Path.Combine(webRootPath, folder);
                    var name        = Path.GetFileNameWithoutExtension(file.FileName);
                    var extension   = Path.GetExtension(file.FileName);
                    //var dateExt = DateTime.Now.ToString("MMddyyyy");
                    var newFileName = $"{lot.LotNumber} - {name}{extension}";

                    // Add (#) to the end of the file if there are files with the same name
                    int copyCount = 0;
                    while (System.IO.File.Exists(Path.Combine(uploads, newFileName)))
                    {
                        copyCount++;
                        newFileName = $"{lot.LotNumber} - {name} ({copyCount}){extension}";
                    }

                    using (var filestream = new FileStream(Path.Combine(uploads, newFileName), FileMode.Create))
                    {
                        file.CopyTo(filestream);
                    }

                    var uploadFile = new SunridgeHOA.Models.File
                    {
                        FileURL          = $@"\{folder}\{newFileName}",
                        Name             = fileDesc.Name,
                        Description      = fileDesc.Description,
                        LastModifiedBy   = loggedInUser.FullName,
                        LastModifiedDate = DateTime.Now
                    };
                    _context.File.Add(uploadFile);

                    var lotHistory = new LotHistory
                    {
                        LotId            = lotId,
                        PrivacyLevel     = "Owner",
                        LastModifiedBy   = loggedInUser.FullName,
                        LastModifiedDate = DateTime.Now,
                        Files            = new List <SunridgeHOA.Models.File> {
                            uploadFile
                        }
                    };
                    _context.LotHistory.Add(lotHistory);
                }


                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(ViewFiles), new { id = lotId }));
            }

            ViewData["LotId"] = lotId;
            return(View());
        }