public async Task AddSportComplex(SportComplexInputViewModel input, string imagePath) { var address = new Address { City = input.AddressCity, Street = input.AddressStreet, NumberOfStreet = input.AddressNumberOfStreet, }; var user = this.userRepository.All().FirstOrDefault(u => u.UserName == input.Username); if (user == null) { throw new ArgumentException("This user is invalid"); } var sportComplex = new SportComplex { Name = input.Name, Address = address, FootballArenasUserId = user.Id, }; this.AddFootballFieldsInternal(input, sportComplex); await this.AddImages(input, sportComplex, imagePath); await this.sportComplexRepository.AddAsync(sportComplex); await this.sportComplexRepository.SaveChangesAsync(); }
private async Task AddImages(SportComplexInputViewModel viewModel, SportComplex sportComplex, string imagePath) { if (viewModel.Images != null) { foreach (var image in viewModel.Images) { var extension = Path.GetExtension(image.FileName).TrimStart('.'); if (!this.allowedExtensions.Any(x => extension.EndsWith(x))) { throw new Exception("Invalid image!"); } var dbImage = new Image { Extension = extension, }; sportComplex.Images.Add(dbImage); var physicalPath = $"{imagePath}/sportComplexes/{dbImage.Id}.{extension}"; await using Stream fileStream = new FileStream(physicalPath, FileMode.Create); await image.CopyToAsync(fileStream); } } }
public IViewComponentResult Invoke() { var viewModel = new SportComplexInputViewModel(); viewModel.Users = this.userService.GetUsersWithoutSportComplex(); return(this.View(viewModel)); }
public async Task AddFootballFields(SportComplexInputViewModel input) { var sportComplex = this.sportComplexRepository.All() .First(s => s.Id == input.Id); this.AddFootballFieldsInternal(input, sportComplex); await this.sportComplexRepository.SaveChangesAsync(); }
public async Task UploadNewImages(SportComplexInputViewModel input, string imagePath) { var sportComplex = this.sportComplexRepository.All().First(s => s.Id == input.Id); await this.AddImages(input, sportComplex, imagePath); await this.sportComplexRepository.SaveChangesAsync(); }
public IActionResult AddFootballFields(int id) { var sportComplex = this.sportComplexService.GetOriginalSportComplexById(id); var viewModel = new SportComplexInputViewModel { Id = sportComplex.Id, }; return(this.View(viewModel)); }
public IActionResult UploadNewImages(int id) { var sportComplex = this.sportComplexService.GetOriginalSportComplexById(id); var viewModel = new SportComplexInputViewModel { Id = sportComplex.Id, }; return(this.View(viewModel)); }
public IActionResult Add() { var viewModel = new SportComplexInputViewModel(); if (this.footballArenaUserService.GetUsersWithoutSportComplex().Count < 1) { return(this.Redirect("/Identity/Account/Register")); } return(this.View(viewModel)); }
public async Task <IActionResult> AddFootballFields(SportComplexInputViewModel input) { try { await this.sportComplexService.AddFootballFields(input); } catch (Exception e) { this.ModelState.AddModelError(string.Empty, e.Message); return(this.View(input)); } return(this.Redirect("/")); }
public async Task <IActionResult> Add(SportComplexInputViewModel viewModel) { try { await this.sportComplexService.AddSportComplex(viewModel, $"{this.environment.WebRootPath}/images"); } catch (Exception e) { this.ModelState.AddModelError(string.Empty, e.Message); return(this.View(viewModel)); } return(this.Redirect("/")); }
public async Task EditFootballFields(SportComplexInputViewModel input) { var fields = this.footballFieldsRepository.All().Where(f => f.SportComplexId == input.Id).ToList(); foreach (var footballField in fields) { var inputEditedField = input.FootballFields.First(f => f.Id == footballField.Id); footballField.Name = inputEditedField.Name; footballField.Size = inputEditedField.Size; footballField.RecommendedNumberOfPeople = inputEditedField.RecommendedNumberOfPeople; await this.footballFieldsRepository.SaveChangesAsync(); } }
public async Task EditSportComplex(SportComplexInputViewModel input) { var sportComplexToEdit = this.sportComplexRepository.All().First(s => s.Id == input.Id); var addressToEdit = this.addressRepository.All().First(a => a.SportComplexId == sportComplexToEdit.Id); addressToEdit.NumberOfStreet = input.AddressNumberOfStreet; addressToEdit.City = input.AddressCity; addressToEdit.Street = input.AddressStreet; await this.addressRepository.SaveChangesAsync(); sportComplexToEdit.Name = input.Name; sportComplexToEdit.Address = addressToEdit; await this.sportComplexRepository.SaveChangesAsync(); }
private void AddFootballFieldsInternal(SportComplexInputViewModel input, SportComplex sportComplex) { if (input.FootballFields != null) { foreach (var fieldInput in input.FootballFields) { if (fieldInput.Name == string.Empty || fieldInput.Size == null) { throw new ArgumentException("Empty football field"); } var field = new FootballField() { Name = fieldInput.Name, Size = fieldInput.Size, RecommendedNumberOfPeople = fieldInput.RecommendedNumberOfPeople, }; sportComplex.FootballFields.Add(field); } } }
public async Task <IActionResult> DeleteFootballFieldsById(SportComplexInputViewModel field) { await this.sportComplexService.DeleteFootballFields(field.Id); return(this.Redirect("/")); }
public async Task <IActionResult> EditFootballField(SportComplexInputViewModel input) { await this.sportComplexService.EditFootballFields(input); return(this.RedirectToAction("EditFootballField", new { id = input.Id })); }
public async Task <IActionResult> UploadNewImages(SportComplexInputViewModel input) { await this.sportComplexService.UploadNewImages(input, $"{this.environment.WebRootPath}/images"); return(this.Redirect("/")); }