public async Task <object> CreatePlaylist(PlaylistViewModel model, string token) { try { var userToken = JwtDecode.User(token); var user = await _context.Users .Where(x => x.UserName == userToken) .Include(x => x.Playlists) .FirstOrDefaultAsync(); var playlist = new Playlist(); playlist.Name = model.Name; string idStringTMP = model.Name.Replace("/", ""); if (idStringTMP.Length >= 8) { idStringTMP = idStringTMP.Substring(0, 8); } playlist.IdString = (idStringTMP + Guid.NewGuid().ToString().Substring(0, 8)).ToLower(); playlist.Id = new Guid(); user.Playlists.Add(playlist); await _context.SaveChangesAsync(); return(new { Succeeded = true }); } catch { return(new { Succeeded = false }); } }
public async Task <object> Add(string token, string idSong) { try { var userToken = JwtDecode.User(token); var user = _context.Users .Where(x => x.UserName == userToken) .Include(x => x.Playlists) .FirstOrDefault(); var favorit = user.Playlists.Where(x => x.IsFavorit == true).FirstOrDefault(); var song = _context.Songs.Where(x => x.IdString == idSong).FirstOrDefault(); favorit.Songs.Add(new SongPlaylist() { Playlist = favorit, Song = song }); _context.SaveChanges(); return(new { Succeeded = true }); } catch (Exception e) { return(new { Succeeded = false, }); } }
public async Task <object> GetByCurrentUser(string token) { try { var userToken = JwtDecode.User(token); var user = await _context.Users.Where(x => x.UserName == userToken) .Include(x => x.Playlists) .FirstOrDefaultAsync(); List <object> result = new List <object>(); foreach (var item in user.Playlists) { result.Add(new { item.Name, item.IdString, item.IsFavorit, item.IsPrivate }); } return(new { Succeeded = true, Playlist = result }); } catch { return(new { Succeeded = false }); } }
public async Task <object> Remove(string title, string token) { try { var user = JwtDecode.User(token); var model = await _context.CarOffers .Where(x => x.User.NormalizedUserName == user.ToUpper()) .Where(x => x.Title == title.ToLower()).FirstOrDefaultAsync(); if (model == null) { return(false); } model.Deleted = true; _context.CarOffers.Update(model); await _context.SaveChangesAsync(); return(new { Succeeded = true }); } catch { return(new { Succeeded = false }); } }
public async Task <object> Get(string token) { try { var userToken = JwtDecode.User(token); var user = await _context.Users .Where(x => x.UserName == userToken) .Include(x => x.Playlists) .ThenInclude(x => x.Songs) .ThenInclude(x => x.Song) .ThenInclude(x => x.SongFile) .Include(x => x.Playlists) .ThenInclude(x => x.Songs) .ThenInclude(x => x.Song) .ThenInclude(x => x.Album) .ThenInclude(x => x.Artist) .FirstOrDefaultAsync(); var favorit = user.Playlists.Where(x => x.IsFavorit == true).FirstOrDefault(); AlbumViewModel album = new AlbumViewModel() { IdString = favorit.IdString, Songs = favorit.Songs.Select(x => new SongViewModel() { Path = x.Song.SongFile.Path, Name = x.Song.Name, Length = x.Song.Length.ToString(), IdString = x.Song.IdString, Album = new AlbumViewModel() { ArtistName = x.Song.Album.Artist.Name, ArtistIdString = x.Song.Album.Artist.IdString, IdString = x.Song.Album.IdString, Name = x.Song.Album.Name } }).ToList() }; return(new { Succeeded = true, Favorit = album }); } catch (Exception e) { return(new { Succeeded = false, }); } }
public async Task <object> ChangePassword(string token, string passwordOld, string passwordNew) { var userName = JwtDecode.User(token); var user = await _userManager.FindByNameAsync(userName); var result = await _userManager.ChangePasswordAsync(user, passwordOld, passwordNew); if (result.Succeeded) { return(new { result.Succeeded, Token = GenerateJwtToken(user.UserName, user), user.FirstName, user.SecondName, user.PhoneNumber }); } return(result); }
public async Task <object> Update(CarOfferViewModel model, string token) { string user = JwtDecode.User(token); var offer = await _context.CarOffers .Where(x => x.User.NormalizedUserName == user.ToUpper()) .Where(x => x.Title == model.Title) .Include(x => x.Equipments) .ThenInclude(x => x.Equipment) .Include(x => x.CarModel) .Include(x => x.CarProducent) .Include(x => x.Fuel) .Include(x => x.Files) .FirstOrDefaultAsync(); if (offer == null) { return new { Succeeded = false, Errors = new[] { new { Code = "NotFound" } } } } ; offer.CarModel = await _context.CarModels.Where(x => x.IdString == model.CarModel).FirstOrDefaultAsync(); offer.CarProducent = await _context.CarProducents.Where(x => x.IdString == model.CarProducent).FirstOrDefaultAsync(); offer.Color = model.Color; offer.Condition = model.Condition; offer.Country = model.Country; offer.Description = model.Description; offer.Door = model.Door; offer.HoursePower = model.HoursePower; offer.Mileage = model.Mileage; offer.PhoneNumber = model.PhoneNumber; offer.Price = model.Price; offer.Seat = model.Seat; offer.ShortDescription = model.ShortDescription; offer.VinNumber = model.VinNumber; offer.Year = model.Year; List <CarOfferEquipment> equipments = new List <CarOfferEquipment>(); var equipmentDB = await _context.Equipments.ToListAsync(); foreach (var item in model.Equipment) { var eqId = equipmentDB.Where(x => x.IdString == item).Select(x => x.Id).FirstOrDefault(); if (eqId != null) { equipments.Add(new CarOfferEquipment() { CarOfferId = offer.Id, EquipmentId = eqId }); } } List <File> files = new List <File>(); foreach (var item in model.FileViewModels) { if (item.Base64 != null) { if (item.Type.Contains("image") && item.Base64.Length > 65) { string hash = (item.Base64.Replace("data:" + item.Type + ";base64,", "").Substring(0, 60) + ObjectId.GenerateNewId().ToString().Substring(0, 8)).Replace("/", "").Replace("-", "").Replace("+", "") + "." + item.Type.Replace("image/", ""); var byteBuffer = Convert.FromBase64String(item.Base64.Replace("data:" + item.Type + ";base64,", "")); var webRoot = _env.WebRootPath; var filePath = System.IO.Path.Combine(webRoot, "images"); filePath = System.IO.Path.Combine(filePath, hash); System.IO.File.WriteAllBytes(filePath, byteBuffer); files.Add(new File() { Id = Guid.NewGuid(), Name = item.Name, Hash = hash, Path = "images\\", Type = item.Type.Split('/').FirstOrDefault() }); } } else if (item.Type == "image" && (item.Base64 == "" || item.Base64 == null)) { var img = await _context.Files.Where(x => (x.Path + x.Hash) == item.Path).FirstOrDefaultAsync(); if (img != null) { files.Add(img); } } } offer.Files = files; offer.Equipments = equipments; _context.Update(offer); await _context.SaveChangesAsync(); return(new { Succeeded = true }); } }
public async Task <object> Save(CarOfferViewModel model, string token) { var offerID = Guid.NewGuid(); var allTitles = await _context.CarOffers.Select(x => x.Title).ToListAsync(); string title; string titleTmp = new string(model.ShortDescription.Select(x => char.IsLetterOrDigit(x) ? x : '-').ToArray()).ToLower(); string check; do { title = titleTmp + ObjectId.GenerateNewId().ToString().Substring(0, 8); check = allTitles.FirstOrDefault(x => x.Contains(title)); } while (check != null); List <File> files = new List <File>(); foreach (var item in model.FileViewModels) { if (item.Type.Contains("image") && item.Base64.Length > 65) { string hash = (item.Base64.Replace("data:" + item.Type + ";base64,", "").Substring(0, 60) + ObjectId.GenerateNewId().ToString().Substring(0, 8)).Replace("/", "").Replace("-", "") + "." + item.Type.Replace("image/", ""); var byteBuffer = Convert.FromBase64String(item.Base64.Replace("data:" + item.Type + ";base64,", "")); var webRoot = _env.WebRootPath; var filePath = System.IO.Path.Combine(webRoot, "images"); filePath = System.IO.Path.Combine(filePath, hash); System.IO.File.WriteAllBytes(filePath, byteBuffer); files.Add(new File() { Id = Guid.NewGuid(), Name = item.Name, Hash = hash, Path = "images\\", Type = item.Type.Split('/').FirstOrDefault() }); } } List <CarOfferEquipment> equipments = new List <CarOfferEquipment>(); var equipmentDB = await _context.Equipments.ToListAsync(); foreach (var item in model.Equipment) { var eqId = equipmentDB.Where(x => x.IdString == item).Select(x => x.Id).FirstOrDefault(); if (eqId != null) { equipments.Add(new CarOfferEquipment() { CarOfferId = offerID, EquipmentId = eqId }); } } var userNameToken = JwtDecode.User(token); var user = await _context.Users.FirstOrDefaultAsync(x => x.NormalizedUserName == userNameToken.ToUpper()); CarOffer carOffer = new CarOffer() { Id = Guid.NewGuid(), CarModel = await _context.CarModels.Where(x => x.ModelName.ToUpper() == model.CarModel.ToUpper()).FirstOrDefaultAsync(), CarProducent = await _context.CarProducents.Where(x => x.ProducentName.ToUpper() == model.CarProducent.ToUpper()).FirstOrDefaultAsync(), Fuel = await _context.FuelTypes.Where(x => x.FuelName.ToUpper() == model.Fuel.ToUpper()).FirstOrDefaultAsync(), Description = model.Description, PhoneNumber = model.PhoneNumber, ShortDescription = model.ShortDescription, Equipments = equipments, Title = title, Year = model.Year, Price = model.Price, VinNumber = model.VinNumber, Files = files, Mileage = model.Mileage, AddDate = DateTime.Now, Condition = model.Condition, Country = model.Country, Door = model.Door, Seat = model.Seat, Color = model.Color, HoursePower = model.HoursePower, User = user }; await _context.CarOffers.AddAsync(carOffer); await _context.SaveChangesAsync(); return(new { Succeeded = true }); }
public async Task <object> GetByUser(string page, string token) { try { IQueryable <CarOffer> query = _context.CarOffers; int itemsOnPage = 2; if (!int.TryParse(page, out int pageNumber)) { return(null); } var user = JwtDecode.User(token); if (string.IsNullOrEmpty(user)) { return(null); } query = query.Where(x => x.User.NormalizedUserName == user.ToUpper()).Include(x => x.CarProducent) .Where(x => x.Deleted == false) .Include(x => x.CarModel) .Include(x => x.User) .Include(x => x.Fuel) .Include(x => x.Files); var countOffers = await query.CountAsync(); var offers = await query.OrderByDescending(x => x.AddDate) .Skip(itemsOnPage * (pageNumber - 1)) .Take(itemsOnPage) .ToListAsync(); var offerList = new List <CarOfferAdverViewModel>(); foreach (var item in offers) { var image = item.Files.FirstOrDefault(); var fileViewModel = new FileViewModel(); if (image != null) { fileViewModel = new FileViewModel() { Path = image.Path + image.Hash, Name = image.Name } } ; offerList.Add(new CarOfferAdverViewModel { ProducentName = item.CarProducent.ProducentName, Fuel = item.Fuel.FuelName, AddDate = item.AddDate, Mileage = item.Mileage, ModelName = item.CarModel.ModelName, Price = item.Price, Title = item.Title, ShortDescription = item.ShortDescription, Year = item.Year, FileViewModel = fileViewModel }); } var pagination = new Pagination { TotalItems = countOffers, ItemsOnPage = itemsOnPage, CurrentyPage = pageNumber }; return(new { offerList, pagination, Succeeded = true }); } catch (Exception ex) { return(new { Succeeded = false }); throw ex; } return(null); }
public async Task <object> ChangeData(string token, UserChangeDataViewModel model) { var userName = JwtDecode.User(token); var user = await _userManager.FindByNameAsync(userName); if (!string.IsNullOrEmpty(model.City)) { user.City = model.City; } if (!string.IsNullOrEmpty(model.Country)) { user.Country = model.Country; } if (!string.IsNullOrEmpty(model.PhoneNumber)) { user.PhoneNumber = model.PhoneNumber; } if (!string.IsNullOrEmpty(model.PostCode)) { user.PostCode = model.PostCode; } if (!string.IsNullOrEmpty(model.Email)) { var userByEmail = await _userManager.FindByEmailAsync(model.Email); if (userByEmail != null) { if (userByEmail.UserName == user.UserName) { user.Email = model.Email; } else { return(new { Succeeded = false, Errors = new[] { new { Code = "BusyEmail", Description = "This Email is busy by other user" } } }); } } } var result = await _userManager.UpdateAsync(user); if (result.Succeeded) { return(new { Token = GenerateJwtToken(user.UserName, user), user.FirstName, user.SecondName, user.PhoneNumber }); } return(result); }