public ApiResponse <ApiModels.AuthUser> UpdateToken(Guid token) { DataModels.User dataUser = _dbContext.Users.FirstOrDefault(x => x.Token == token); if (dataUser == null) { return(new ApiResponse <ApiModels.AuthUser>("User with this token not found")); } dataUser.Token = Guid.NewGuid(); dataUser.ExpiredAt = DateTime.UtcNow.AddDays(TokenExpiredAtDays); _dbContext.SaveChanges(); return(new ApiResponse <ApiModels.AuthUser>() { Ok = true, Data = ApiMapper.GetAuthUser(dataUser), }); }
public ApiResponse <object> LoadAudios(DataModels.User user) { //get and remove all current user's albums/audios List <DataModels.Album> dataAlbumList = _dbContext.Albums.Where(x => x.UserId == user.Id).ToList(); List <DataModels.Audio> dataAudioList = _dbContext.Audios.Where(x => x.UserId == user.Id).ToList(); IEnumerable <int> audioIdList = dataAudioList.Select(x => x.VkId); List <DataModels.MixAudio> mixDataAudioList = _dbContext.MixAudios.Where(x => audioIdList.Contains(x.AudioId)).ToList(); //Task loadAlbums = Task.Run(() => // dataAlbumList = _dbContext.Albums.Where(x => x.UserId == user.Id).ToList()); //Task loadAudios = Task.Run(() => // dataAudioList = _dbContext.Audios.Where(x => x.UserId == user.Id).ToList()); //await Task.WhenAll(loadAlbums, loadAudios); _dbContext.MixAudios.RemoveRange(mixDataAudioList); _dbContext.Audios.RemoveRange(dataAudioList); _dbContext.Albums.RemoveRange(dataAlbumList); //download all (okey, 6000) VkModels.AlbumEtity vkAlbumEntity = null; VkModels.AudioEtity vkAudioEntity = null; try { vkAlbumEntity = _vkWebService.GetAlbums(user.AccessToken, 100); } catch (Exception ex) { return(new ApiResponse <object>(ex.Message)); } try { //TODO: mb add few Tasks for better perform? Need to check vkAudioEntity = _vkWebService.GetAudios(user.AccessToken, 6000); } catch (Exception ex) { return(new ApiResponse <object>(ex.Message)); } //album errors if (vkAlbumEntity.error != null) { return(new ApiResponse <object>(vkAlbumEntity.error?.error_msg)); } if (vkAlbumEntity.response == null) { return(new ApiResponse <object>("Album response is null")); } //audio errors if (vkAudioEntity.error != null) { return(new ApiResponse <object>(vkAudioEntity.error?.error_msg)); } if (vkAudioEntity.response == null) { return(new ApiResponse <object>("Audio response is null")); } user.AlbumsCount = vkAlbumEntity.response.items.Count(); user.AudiosCount = vkAudioEntity.response.items.Count(); _dbContext.Entry(user).State = System.Data.Entity.EntityState.Modified; //map albums IEnumerable <DataModels.Album> albums = vkAlbumEntity .response .items .Select(x => { DataModels.Album dataAlbum = VkMapper.GetAlbum(x); dataAlbum.UserId = user.Id; return(dataAlbum); }); //map musics var audioList = new List <DataModels.Audio>(vkAudioEntity.response.items.Count()); for (int i = 0; i < vkAudioEntity.response.items.Count(); i++) { var mappedAudio = VkMapper.GetAudio(vkAudioEntity.response.items.ElementAt(i)); mappedAudio.Order = i + 1; mappedAudio.UserId = user.Id; audioList.Add(mappedAudio); } //add ranges and save changes _dbContext.Albums.AddRange(albums); _dbContext.SaveChanges(); foreach (var aud in audioList) { DataModels.Album album = _dbContext.Albums.Local.FirstOrDefault(x => aud.AlbumId == x.VkId); if (album != null) { aud.AlbumId = album.Id; } } _dbContext.Audios.AddRange(audioList); _dbContext.SaveChanges(); return(new ApiResponse <object>() { Ok = true }); }
public ApiResponse <object> CreateSchedule(DataModels.User dataUser, Schedule schedule) { if (schedule.Title == null) { return(new ApiResponse <object>("Schedule's title can't be null")); } schedule.Title = schedule.Title.Trim(); if (schedule.Title == string.Empty) { return(new ApiResponse <object>("Schedule's title can't be empty")); } if (schedule.Title.Length > 50) { return(new ApiResponse <object>("Schedule's title can't be greater then 50 symbols")); } if (schedule.ScheduleRows == null || schedule.ScheduleRows.Count() == 0) { return(new ApiResponse <object>("Schedule rows can't be null or empty")); } IEnumerable <int> orderList = schedule.ScheduleRows.Select(x => x.Order); List <int> sortOrderList = orderList.OrderBy(x => x).ToList(); int sortOrderCount = sortOrderList.Last() - sortOrderList.First() + 1; if (sortOrderCount > sortOrderList.Count) { return(new ApiResponse <object>("Order list has gaps")); } if (sortOrderCount < sortOrderList.Count) { return(new ApiResponse <object>("Order list has duplicates")); } IEnumerable <IGrouping <int, ScheduleRow> > scheduleRowsGroupIndex = schedule.ScheduleRows.GroupBy(x => x.UserId); //foreach (var sr in scheduleRowsGroupIndex) //{ // if (sr.GroupBy(x => x.CreateRandomOrder).Count() > 1) // return new ApiResponse<object>($"User {sr.Key} have more then one order"); //} DataModels.Schedule dataSchedule = new DataModels.Schedule() { BlockSize = sortOrderList.Count, DateCreated = DateTime.UtcNow, Title = schedule.Title, UserId = dataUser.Id, }; _dbContext.Schedules.Add(dataSchedule); _dbContext.SaveChanges(); int addedScheduleId = dataSchedule.Id; var dataScheduleRowList = new List <DataModels.ScheduleRow>(); foreach (var sr in scheduleRowsGroupIndex) { ScheduleRow firstScheduleRow = sr.First(); //if(firstScheduleRow.CreateRandomOrder) //{ // CreateRandomOrderForUser(firstScheduleRow.UserId, addedScheduleId); //} for (int i = 0; i < sr.Count(); i++) { dataScheduleRowList.Add(new DataModels.ScheduleRow() { GroupIndex = i + 1, Order = sr.ElementAt(i).Order, ScheduleId = addedScheduleId, UserId = firstScheduleRow.UserId, }); } } _dbContext.ScheduleRows.AddRange(dataScheduleRowList); _dbContext.SaveChanges(); return(new ApiResponse <object>() { Ok = true, Data = dataSchedule, }); }