Beispiel #1
0
        public void Edit_Normal_Conditions()
        {
            var repo     = new CandidacyRepository();
            var user     = new UserDetails();
            var userRepo = new UserDetailsRepository();

            userRepo.Add(user);
            var candidacy = new Candidacy("John", "empty description", "empty", user);

            repo.Add(candidacy);
            var temp = repo.GetAll().FirstOrDefault();

            temp.PhotoPath   = "foo";
            temp.Description = "bar";
            repo.Edit(temp);
        }
Beispiel #2
0
        public void Edit_PhotoPath_Null()
        {
            var repo     = new CandidacyRepository();
            var user     = new UserDetails();
            var userRepo = new UserDetailsRepository();

            userRepo.Add(user);
            var candidacy = new Candidacy("John", "empty description", "empty", user);

            repo.Add(candidacy);
            var temp = repo.GetAll().FirstOrDefault();

            Assert.Throws <ArgumentNullException>(() =>
            {
                temp.PhotoPath   = null;
                temp.Description = "bar";
                repo.Edit(temp);
            });
        }
        public async Task <IActionResult> Add()
        {
            var token = HttpContext.Request.Headers["Authorization"].Last().Split(" ").Last();

            string[] roles   = { "User" };
            var      handler = new JwtSecurityTokenHandler();

            if (RoleService.CheckRoles(token, roles, _userManager))
            {
                var httpRequest = HttpContext.Request;
                var file        = httpRequest.Body;

                //checks the size of file
                var imageHandler = new ImageSecurityHandler();
                if (!imageHandler.CheckFileSize(httpRequest.ContentLength.Value))
                {
                    _logger.LogInformation($"size is {httpRequest.ContentLength}");
                    return(BadRequest("Photo must be between 5KB and 5MB"));
                }
                //checks the format of file
                if (!imageHandler.CheckFileFormat(httpRequest.ContentType))
                {
                    _logger.LogInformation($"file format is {httpRequest.ContentType}");
                    return(BadRequest("Wrong file format"));
                }

                var sub = handler.ReadJwtToken(token).Payload.Sub;

                var credentials =
                    GoogleCredential.FromFile("../Infrastructure/Images/GCStorage/Rosta-a2299c0ab851.json");
                var storage = StorageClient.CreateAsync(credentials);

                var lastId = 0;
                if (storage.Result
                    .ListObjects("deep-castle-261418-user-photo-bucket")
                    .Select(x => x.Name)
                    .Count(x => x.Contains(sub)) > 0)
                {
                    lastId = int.Parse(storage.Result
                                       .ListObjects("deep-castle-261418-user-photo-bucket")
                                       .Select(x => x.Name).Last(x => x.Contains(sub))
                                       .Split("-").Last());
                }


                var detailsRepository   = new UserDetailsRepository();
                var details             = detailsRepository.GetByUserId(sub);
                var candidacyRepository = new CandidacyRepository();
                var candidacy           = candidacyRepository.GetAll().Last(x => x.OwnerId == details.Id);

                //Checks if User have candidacy
                if (candidacyRepository.GetAll().Count(x => x.OwnerId == details.Id) == 0)
                {
                    return(BadRequest("User didnt submited candidacy."));
                }

                //Uploading Photo to Google Cloud and updating indecies.
                var photoName = $"{sub}-profilePhoto-{lastId + 1}";
                storage.Result.UploadObject("deep-castle-261418-user-photo-bucket", photoName,
                                            MediaTypeNames.Image.Jpeg, file, null);

                candidacy.PhotoPath = photoName;
                candidacyRepository.Edit(candidacy);

                return(Ok());
            }

            return(Unauthorized());
        }