/// <summary>
        /// Create a photo.
        /// </summary>
        /// <param name="newphotoDTO">Details of photo to be created.</param>
        /// <returns>Returns created photo or an appropriate error message.</returns>
        public async Task <PhotoDTO> CreateAsync(NewPhotoDTO newphotoDTO)
        {
            if (newphotoDTO.Title == null)
            {
                throw new ArgumentException(Exceptions.RequiredPhotoName);
            }
            if (newphotoDTO.Description == null)
            {
                throw new ArgumentException(Exceptions.RequiredPhotoDescription);
            }
            if (newphotoDTO.PhotoUrl == null)
            {
                throw new ArgumentException(Exceptions.RequiredPhotoURL);
            }
            if (newphotoDTO.ContestName == null)
            {
                throw new ArgumentException(Exceptions.InvalidContestName);
            }
            var contest = await this.contestService.FindContestByNameAsync(newphotoDTO.ContestName);

            if (contest.Status.Name != "Phase 1")
            {
                throw new ArgumentException(Exceptions.ClosedContest);
            }
            var username = this.userManager.GetUserName(this.signInManager.Context.User);
            var user     = await this.userService.GetUserByUsernameAsync(username);

            if (await this.dbContext.Juries.FirstOrDefaultAsync(j => j.UserId == user.Id && j.ContestId == contest.Id) != null)
            {
                throw new ArgumentException(Exceptions.ExistingJury);
            }
            var userContests = await this.userContestService.GetAllUserContestsAsync();

            var userContest = userContests.FirstOrDefault(uc => uc.UserId == user.Id && uc.ContestId == contest.Id)
                              ?? throw new ArgumentException(Exceptions.NotEnrolledInContest);

            if (userContest.HasUploadedPhoto)
            {
                throw new ArgumentException(Exceptions.AlreadyUploadedAPhoto);
            }
            var photo = new Photo()
            {
                Title       = newphotoDTO.Title,
                Description = newphotoDTO.Description,
                PhotoUrl    = newphotoDTO.PhotoUrl,
                ContestId   = contest.Id,
                UserId      = user.Id,
                CreatedOn   = DateTime.UtcNow,
            };

            userContest.HasUploadedPhoto = true;
            await this.dbContext.Photos.AddAsync(photo);

            await this.dbContext.SaveChangesAsync();

            return(new PhotoDTO(photo));
        }
Beispiel #2
0
        public async Task <IActionResult> CreateAsync([FromBody] NewPhotoDTO newPhotoDTO)
        {
            try
            {
                var photo = await this.photoService.CreateApiAsync(newPhotoDTO);

                return(Created("post", photo));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
Beispiel #3
0
        public async Task <IActionResult> Create(CreatePhotoViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var newFileName = $"{Guid.NewGuid()}_{model.File.FileName}";
                    var fileDbPath  = $"/Images/{newFileName}";
                    var saveFile    = Path.Combine(webHostEnvironment.WebRootPath, "Images", newFileName);
                    using (var fileSelected = new FileStream(saveFile, FileMode.Create))
                    {
                        await model.File.CopyToAsync(fileSelected);
                    }
                    using (var img = Image.Load(model.File.OpenReadStream()))
                    {
                        img.Mutate(x => x.Resize(400, 400));
                        //img.Save(fileDbPath);
                    }

                    var newPhotoDTO = new NewPhotoDTO()
                    {
                        Title       = model.Title,
                        Description = model.Description,
                        PhotoUrl    = fileDbPath,
                        ContestName = model.ContestName
                    };


                    await this.photoService.CreateAsync(newPhotoDTO);

                    return(RedirectToAction("GetUserContests", "Contests"));
                }
                catch (Exception e)
                {
                    toastNotification.AddErrorToastMessage(e.Message, new NotyOptions());
                    var path = Request.Path.Value.ToString() + "?contestName=" + model.ContestName;
                    return(Redirect(path));
                }
            }
            return(View(model));
        }