Exemple #1
0
        /* <summary>
         * Checks if entity already exists and add's a new Entity if it's not the case.
         * </summary> */
        public async Task <T> Add(T entity)
        {
            var dbCheck = _worldExplorerContext.Set <T>().ToList();

            foreach (var dbObject in dbCheck)
            {
                if (dbObject == entity)
                {
                    return(null);
                }
            }

            await _worldExplorerContext.SaveChangesAsync();

            return(entity);
        }
Exemple #2
0
        public async Task <IActionResult> PostPhotoMemory([FromRoute] Guid countryId, [FromRoute] Guid userId, IFormFile file)
        {
            var entity = await _mappingRepository.GetById(countryId);

            if (entity == null)
            {
                return(NotFound($"Cannot find country with id: {countryId}!!"));
            }
            ;
            try
            {
                var filename = await _memoryPhotoService.CreateImage(file, Guid.NewGuid(), nameof(Country).ToLower());

                var memory = new PhotoMemory()
                {
                    Id       = Guid.NewGuid(),
                    UserId   = userId,
                    FileName = filename.ToString()
                };

                _worldExplorerContext.Memories.Add(memory);
                await _worldExplorerContext.SaveChangesAsync();

                entity.CountryPhotoMemories.Add(
                    new PhotoMemoryDto
                {
                    Id       = memory.Id,
                    FileName = memory.FileName,
                    UserId   = userId
                });

                return(Ok(await _countryMapperRepo.Update(countryId.ToString(), entity)));
            }
            catch
            {
                return(BadRequest("Cannot save image!!"));
            }
        }
Exemple #3
0
        public async Task <IActionResult> addUser([FromBody] UserDto userDto)
        {
            var existingUser = _context.Users.FirstOrDefault(m => m.Email == userDto.Email);

            if (existingUser != null)
            {
                return(BadRequest("User already exists"));
            }

            try
            {
                var hasher = new PasswordHasher <User>();
                var user   = new User()
                {
                    Id             = Guid.NewGuid().ToString(),
                    FirstName      = userDto.FirstName,
                    LastName       = userDto.LastName,
                    Nationality    = userDto.Nationality,
                    Email          = userDto.Email,
                    BirthDate      = userDto.BirthDate,
                    EmailConfirmed = true,
                };
                user.PasswordHash = hasher.HashPassword(user, userDto.Password);
                await _context.Users.AddAsync(user);

                await _context.SaveChangesAsync();

                _context.UserClaims.Add(new IdentityUserClaim <string>()
                {
                    UserId = user.Id, ClaimType = "role", ClaimValue = "Visitor"
                });
                if (userDto.IsSpotifyDj == true)
                {
                    _context.UserClaims.Add(new IdentityUserClaim <string>()
                    {
                        UserId = user.Id, ClaimType = "isSpotifyDj", ClaimValue = "true"
                    });
                }
                await _context.SaveChangesAsync();

                userDto.Token = _userService.GenerateToken(user);
                return(Ok(userDto));
            }
            catch
            {
                return(BadRequest("User cannot be added"));
            }
        }