コード例 #1
0
        public async Task <IActionResult> Create(NoteViewModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(BadRequest(WebConstants.EnterValidData));
            }

            try
            {
                string imageName = null;

                if (model.NoteImage != null)
                {
                    imageName = optimizer.OptimizeImage(model.NoteImage, 600, 800);
                }
                var userId = this.wrapper.GetLoggedUserId(User);

                var user = await this.userService.GetUserByIdAsync(userId);

                if (!user.CurrentLogbookId.HasValue)
                {
                    return(BadRequest(string.Format(WebConstants.NoLogbookChoosen)));
                }

                var note = await this.noteService.CreateNoteAsync(userId, user.CurrentLogbookId.Value,
                                                                  model.Description, imageName, model.CategoryId);

                if (note.Description == model.Description)
                {
                    return(Ok(string.Format(WebConstants.NoteCreated)));
                }

                return(BadRequest(WebConstants.UnableToCreateNote));
            }
            catch (ArgumentException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (NotFoundException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (NotAuthorizedException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (Exception ex)
            {
                log.Error("Unexpected exception occured:", ex);
                return(RedirectToAction("Error", "Home"));
            }
        }
コード例 #2
0
        public async Task <IActionResult> Create(MovieViewModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(View(model));
            }

            try
            {
                if (await this.movieService.IsMovieExistAsync(model.Title))
                {
                    StatusMessage = string.Format(WebConstants.MovieAlreadyExists, model.Title);

                    return(RedirectToAction("Create", "Movies"));
                }

                StatusMessage = string.Format(WebConstants.MovieSuccessfullyCreated, model.Title);

                string posterName = null;
                string sliderName = null;

                if (model.PosterImage != null)
                {
                    posterName = optimizer.OptimizeImage(model.PosterImage, 268, 182);
                }

                if (model.SliderPoster != null)
                {
                    sliderName = optimizer.OptimizeImage(model.SliderPoster, 500, 1300);
                }

                var movie = await this.movieService
                            .CreateMovieAsync(model.Title, model.Trailer, posterName, sliderName, model.Description, model.ReleaseDate, model.UserId);

                return(RedirectToAction("Details", "Movies", new { id = movie.Id }));
            }

            catch (ArgumentException ex)
            {
                StatusMessage = ex.Message;
                return(RedirectToAction("Create", "Movies"));
            }
        }
コード例 #3
0
        public async Task <IActionResult> Create(BusinessUnitViewModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(BadRequest(WebConstants.EnterValidData));
            }

            try
            {
                string imageName = null;

                if (model.BusinessUnitPicture != null)
                {
                    imageName = optimizer.OptimizeImage(model.BusinessUnitPicture, 400, 800);
                }

                var businessUnit = await this.businessUnitService.CreateBusinnesUnitAsync(model.Name, model.Address, model.PhoneNumber, model.Email, model.Information, model.CategoryId, model.TownId, imageName);

                if (businessUnit.Name == model.Name)
                {
                    return(Ok(string.Format(WebConstants.BusinessUnitCreated, model.Name)));
                }

                return(BadRequest(string.Format(WebConstants.BusinessUnitNotCreated, model.Name)));
            }

            catch (ArgumentException ex)
            {
                return(BadRequest(ex.Message));
            }

            catch (AlreadyExistsException ex)
            {
                return(BadRequest(ex.Message));
            }

            catch (Exception ex)
            {
                log.Error("Unexpected exception occured:", ex);
                return(RedirectToAction("Error", "Home"));
            }
        }
コード例 #4
0
        public async Task <IActionResult> Create(LogbookViewModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(BadRequest(WebConstants.EnterValidData));
            }

            try
            {
                string imageName = null;

                if (model.LogbookPicture != null)
                {
                    imageName = optimizer.OptimizeImage(model.LogbookPicture, 400, 800);
                }

                var logbook = await this.logbookService.CreateLogbookAsync(model.Name, model.BusinessUnitId, imageName);

                if (logbook.Name == model.Name)
                {
                    return(Ok(string.Format(WebConstants.LogbookCreated, model.Name)));
                }

                return(BadRequest(string.Format(WebConstants.LogbookNotCreated, model.Name)));
            }

            catch (ArgumentException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (AlreadyExistsException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (Exception ex)
            {
                log.Error("Unexpected exception occured:", ex);
                return(RedirectToAction("Error", "Home"));
            }
        }
コード例 #5
0
        public async Task <IActionResult> Create(ActorViewModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(View(model));
            }

            try
            {
                string imageName = null;

                if (model.ActorPicture != null)
                {
                    imageName = optimizer.OptimizeImage(model.ActorPicture, 268, 182);
                }

                var actor = await this.actorService.FindActorByNameAsync(model.FirstName, model.LastName);

                if (actor != null)
                {
                    StatusMessage = string.Format(WebConstants.ActorAlreadyExists, model.FirstName, model.LastName);
                    return(RedirectToAction("Create", "Actors"));
                }
                actor = await this.actorService
                        .CreateActorAsync(model.FirstName, model.LastName, imageName, model.Biography);

                if (actor.FirstName == model.FirstName && actor.LastName == model.LastName &&
                    actor.Picture == imageName && actor.Biography == model.Biography)
                {
                    StatusMessage = string.Format(WebConstants.ActorCreated, model.FirstName, model.LastName);
                }
                return(RedirectToAction("Details", "Actors", new { id = actor.Id }));
            }

            catch (ArgumentException ex)
            {
                StatusMessage = ex.Message;
                return(RedirectToAction("Create", "Actors"));
            }
        }
コード例 #6
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;

            if (ModelState.IsValid)
            {
                string imageName = null;

                if (model.UserImage != null)
                {
                    imageName = optimizer.OptimizeImage(model.UserImage, 400, 800);
                }

                if (model.Image != null)
                {
                    optimizer.DeleteOldImage(model.Image);
                }
                var user = new User {
                    Email = model.Email, UserName = model.UserName, Picture = imageName
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    if (model.UserRole == "Manager")
                    {
                        await _userManager.AddToRoleAsync(user, "Manager");
                    }
                    else if (model.UserRole == "Moderator")
                    {
                        await _userManager.AddToRoleAsync(user, "Moderator");
                    }
                    else if (model.UserRole == "Admin")
                    {
                        await _userManager.AddToRoleAsync(user, "Admin");
                    }
                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
                    await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);

                    _logger.LogInformation("User created a new account with password.");
                    return(Ok(string.Format(WebConstants.UserCreated, model.UserName)));
                }
                return(BadRequest("Invalid attempt."));
            }

            return(BadRequest(WebConstants.EnterValidData));
        }
コード例 #7
0
        public async Task <IActionResult> UploadImage()
        {
            var fileList = new List <string>();

            try
            {
                if (HttpContext.Request.Form.Files == null || HttpContext.Request.Form.Files.Count <= 0)
                {
                    return(BadRequest());
                }
                foreach (var file in HttpContext.Request.Form.Files)
                {
                    if (file.Length <= 0)
                    {
                        continue;
                    }

                    var fileName     = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.ToString().Trim('"');
                    var filePath     = Path.Combine(_localImageUploadPath, fileName);
                    var originalPath = filePath + Globals.OriginalFileSuffix;
                    await using (var memoryStream = new MemoryStream())
                    {
                        var sourImage = memoryStream.ToArray();
                        await file.CopyToAsync(memoryStream);

                        SaveFile(sourImage, originalPath);
                        sourImage = memoryStream.ToArray();
                        var optimizedImage = _imageOptimizer.OptimizeImage(sourImage);
                        SaveFile(optimizedImage, filePath);
                    }

                    fileList.Add(Globals.SiteAssetsPath.Replace("~", "") + Globals.ImagesFolder + "/" + fileName);
                }
                return(Ok(fileList));
            }
            catch (Exception ex)
            {
                _logger.LogError(string.Format("Error occured while uploading images"), ex);
                return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
            }
        }