Esempio n. 1
0
        public IHttpResponse AddGame(IHttpRequest req)
        {
            const string formTitleKey       = "title";
            const string formDescriptionKey = "description";
            const string formImageKey       = "imageThumbnail";
            const string formSizeKey        = "size";
            const string formPriceKey       = "price";
            const string formVideoUrlKey    = "videoUrl";
            const string formReleaseDateKey = "releaseDate";

            if (!req.FormData.ContainsKey(formTitleKey) ||
                !req.FormData.ContainsKey(formDescriptionKey) ||
                !req.FormData.ContainsKey(formImageKey) ||
                !req.FormData.ContainsKey(formPriceKey) ||
                !req.FormData.ContainsKey(formSizeKey) ||
                !req.FormData.ContainsKey(formVideoUrlKey) ||
                !req.FormData.ContainsKey(formReleaseDateKey))
            {
                return(this.FileViewResponse(@"Game\add-game"));
            }

            var      title       = req.FormData[formTitleKey];
            var      description = req.FormData[formDescriptionKey];
            var      imageUrl    = req.FormData[formImageKey];
            decimal  price       = decimal.Parse(req.FormData[formPriceKey]);
            decimal  size        = decimal.Parse(req.FormData[formSizeKey]);
            var      videoUrl    = req.FormData[formVideoUrlKey];
            DateTime releaseDate = Convert.ToDateTime(req.FormData[formReleaseDateKey].ToString());

            if (string.IsNullOrWhiteSpace(title) ||
                string.IsNullOrWhiteSpace(description) ||
                string.IsNullOrWhiteSpace(imageUrl) ||
                string.IsNullOrWhiteSpace(price.ToString()) ||
                string.IsNullOrWhiteSpace(size.ToString()) ||
                string.IsNullOrWhiteSpace(videoUrl) ||
                string.IsNullOrWhiteSpace(req.FormData[formReleaseDateKey].ToString()))
            {
                return(this.FileViewResponse(@"Game\add-game"));
            }

            if (Char.IsUpper(title[0]) && title.Length > 3 && title.Length > 100)
            {
                return(this.FileViewResponse(@"Game\add-game"));
            }

            if (price < 0)
            {
                return(this.FileViewResponse(@"Game\add-game"));
            }

            if (size < 0)
            {
                return(this.FileViewResponse(@"Game\add-game"));
            }

            if (videoUrl.Length != 11)
            {
                return(this.FileViewResponse(@"Game\add-game"));
            }

            if (!imageUrl.StartsWith("https://") && !imageUrl.StartsWith("http://"))
            {
                return(this.FileViewResponse(@"Game\add-game"));
            }

            if (description.Length < 20)
            {
                return(this.FileViewResponse(@"Game\add-game"));
            }

            int currentUserId = req.Session.Get <int>(SessionStore.CurrentUserKey);

            User currentUser = userService.GetUserById(currentUserId);

            Game game = new Game()
            {
                Description    = description,
                ImageThumbnail = imageUrl,
                Price          = price,
                ReleaseDate    = releaseDate,
                Size           = size,
                Title          = title,
                Trailer        = videoUrl,
                CreatorId      = currentUserId,
            };

            gameService.AddGameToDb(game);

            return(new RedirectResponse("/games"));
        }