示例#1
0
        public async Task <ActionResult <League> > PostLeague([FromForm] LeaguesAddModel model)
        {
            try {
                League league = new League();
                league.Name            = model.Name;
                league.Country         = model.Country;
                league.FoundedYear     = int.Parse(model.FoundedYear);
                league.MaxNrTeam       = int.Parse(model.MaxNrTeam);
                league.TvPartner       = model.TvPartner;
                league.CurrentChampion = model.CurrentChampion;
                if (model.Logo != null)
                {
                    var filePath = Path.Combine(_env.WebRootPath, "Upload", "Leagues");;

                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    var fullPath = Path.Combine(_env.WebRootPath, "Upload", "Leagues", model.Logo.FileName);
                    using (var stream = new FileStream(fullPath, FileMode.Create))
                    {
                        model.Logo.CopyTo(stream);
                    }

                    league.Logo = "https://localhost:5001/Upload/Leagues/" + model.Logo.FileName;
                }
                else
                {
                    league.Logo = "Empty";
                }


                _context.Leagues.Add(league);
                await _context.SaveChangesAsync();

                return(Ok());
            }
            catch (Exception) {
                return(StatusCode(StatusCodes.Status400BadRequest));
            }
        }
示例#2
0
        public async Task <IActionResult> PutLeague([FromForm] LeaguesAddModel model)
        {
            League league = new League();

            league.Id              = int.Parse(model.Id);
            league.Name            = model.Name;
            league.Country         = model.Country;
            league.FoundedYear     = int.Parse(model.FoundedYear);
            league.MaxNrTeam       = int.Parse(model.MaxNrTeam);
            league.TvPartner       = model.TvPartner;
            league.CurrentChampion = model.CurrentChampion;
            if (model.Logo != null)
            {
                var filePath = Path.Combine(_env.WebRootPath, "Upload", "Leagues");;

                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                var fullPath = Path.Combine(_env.WebRootPath, "Upload", "Leagues", model.Logo.FileName);
                using (var stream = new FileStream(fullPath, FileMode.Create))
                {
                    model.Logo.CopyTo(stream);
                }

                league.Logo = "https://localhost:5001/Upload/Leagues/" + model.Logo.FileName;
            }

            if (!LeagueExists(league.Id))
            {
                return(NotFound());
            }
            else
            {
                _context.Leagues.Update(league);
                await _context.SaveChangesAsync();
            }
            return(Ok());
        }