Esempio n. 1
0
        public async Task UpdateFamilies(MusicFamilyUpdateModel[] model)
        {
            try
            {
                //Updates music families
                var familyNames         = model.Select(i => i.FamilyName).ToHashSet();
                var existingFamilyNames = await _context.MusicFamilies.Select(f => f.Name).ToArrayAsync();

                foreach (var family in familyNames)
                {
                    if (!existingFamilyNames.Contains(family))
                    {
                        await _context.MusicFamilies.AddAsync(new MusicFamily
                        {
                            Id   = Guid.NewGuid().ToString("D"),
                            Name = family
                        });
                    }
                }

                await _context.SaveChangesAsync();

                var existingFamilies = await _context.MusicFamilies.ToArrayAsync();

                //Updates music genres and associate their families
                var genres         = model.Select(i => i).ToHashSet();
                var existingGenres = await _context.MusicGenres.ToArrayAsync();

                foreach (var genre in genres)
                {
                    //If genre allready exist update family id
                    if (existingGenres.Any(g => g.Name.Equals(genre.FamilyName)))
                    {
                        existingGenres.Single(g => g.Name.Equals(genre.FamilyName)).FamilyId =
                            existingFamilies.Single(f => f.Name.Equals(genre.FamilyName)).Id;
                    }
                    else
                    {
                        await _context.MusicGenres.AddAsync(new MusicGenre
                        {
                            Id       = Guid.NewGuid().ToString("D"),
                            Name     = genre.GenreName,
                            FamilyId = existingFamilies.Single(f => f.Name.Equals(genre.FamilyName)).Id
                        });
                    }
                }

                await _context.SaveChangesAsync();
            }
            catch (Exception e)
            {
                throw new HttpStatusCodeException(StatusCodes.Status500InternalServerError, e.Message);
            }
        }
        public async Task <Guid> CreateUser(UserModel userModelModel)
        {
            try
            {
                userModelModel.Id       = Guid.NewGuid();
                userModelModel.Password = PasswordTool.HashPassword(userModelModel.Password);
                await _context.Users.AddAsync(userModelModel);

                await _context.SaveChangesAsync();

                return(userModelModel.Id);
            }
            catch (DbUpdateException e)
            {
                if (e.InnerException != null && e.InnerException.Message.Contains("uplicate"))
                {
                    throw new HttpStatusCodeException(StatusCodes.Status409Conflict, e.InnerException.Message);
                }
                throw new HttpStatusCodeException(StatusCodes.Status400BadRequest, e.Message);
            }
            catch (Exception e)
            {
                throw new HttpStatusCodeException(StatusCodes.Status500InternalServerError, e.Message);
            }
        }