Example #1
0
        public async Task <IActionResult> Create()
        {
            SportsCenterCreateReq req = new SportsCenterCreateReq();

            IEnumerable <SportGetListResItem> sports = await _sports.GetListAsync(new SportGetListReq { });

            req.AvailableSports = new List <System.Web.Mvc.SelectListItem>();

            foreach (SportGetListResItem sport in sports)
            {
                req.AvailableSports.Add(new System.Web.Mvc.SelectListItem {
                    Text = sport.Name, Value = sport.Name
                });
            }

            return(View(req));
        }
        public async Task <SportsCenterCreateRes> CreateAsync(SportsCenterCreateReq req)
        {
            SportsCenter sportsCenter = new SportsCenter
            {
                Name        = req.Name,
                PhotoUrl    = req.PhotoUrl,
                Description = req.Description,
            };

            _dbContext.SportsCenters.Add(sportsCenter);
            await _dbContext.SaveChangesAsync();

            #region Handle Fields

            IEnumerable <SportGetListResItem> sports = await _sports.GetListAsync(new SportGetListReq { });

            foreach (string sport in req.SelectedSports)
            {
                int sportId = sports
                              .Where(s => s.Name == sport)
                              .Select(s => s.Id)
                              .FirstOrDefault();

                Field field = new Field
                {
                    SportId        = sportId,
                    SportsCenterId = sportsCenter.Id
                };

                _dbContext.Fields.Add(field);
            }

            await _dbContext.SaveChangesAsync();


            #endregion

            return(new SportsCenterCreateRes {
                Id = sportsCenter.Id
            });
        }
Example #3
0
        public async Task <IActionResult> Create([FromForm] SportsCenterCreateReq req)
        {
            SportsCenterCreateRes res = await _sportsCenters.CreateAsync(req);

            return(RedirectToAction("List"));
        }