public HttpResponseMessage AddToFavourites(UserFavourite userFavourite) { if (!User.Identity.IsAuthenticated) { return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Unauthorized Request")); } try { UserFavourite uf = new UserFavourite(); uf.AspNetUserId = new Guid(User.Identity.GetUserId()); uf.ReleaseId = userFavourite.ReleaseId; uf.Title = userFavourite.Title; uf.ReleaseYear = userFavourite.ReleaseYear; uf.Label = userFavourite.Label; uf.NoOfTracks = userFavourite.NoOfTracks; uf.ModifiedOn = DateTime.Now; uf.IsActive = userFavourite.IsActive; var result = false; Repository.AddOrUpdateUserFavourite(uf); result = Repository.SaveAll(); return(Request.CreateResponse(HttpStatusCode.OK, result)); } catch (Exception) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Something went wrong")); } }
public bool AddOrUpdateUserFavourite(UserFavourite userFavourite) { if (userFavourite == null) { return(false); } try { UserFavourite _userFavourite = _db.Query <UserFavourite>().Where(u => u.AspNetUserId == userFavourite.AspNetUserId && u.ReleaseId == userFavourite.ReleaseId).FirstOrDefault(); if (_userFavourite != null) { _userFavourite.Title = userFavourite.Title; _userFavourite.ReleaseYear = userFavourite.ReleaseYear; _userFavourite.Label = userFavourite.Label; _userFavourite.NoOfTracks = userFavourite.NoOfTracks; _userFavourite.ModifiedOn = userFavourite.ModifiedOn; _userFavourite.IsActive = userFavourite.IsActive; _db.Update <UserFavourite>(_userFavourite); } else { _db.Add <UserFavourite>(userFavourite); } return(true); } catch (Exception) { return(false); } }
public async Task <ActionResult> Post(UserFavourite userFavourite) { context.Add(userFavourite); await context.SaveChangesAsync(); return(Ok()); }
public async Task <string> AddMovieToFavouritesAsync(int movieId, string username) { var user = this.usersRepository.All() .Where(x => x.UserName == username) .FirstOrDefault(); var checkIfExists = this.userFavouriteRepository.All() .Where(x => x.ApplicationUserId == user.Id && x.MovieId == movieId) .FirstOrDefault(); var movie = await this.moviesRepository.GetByIdWithDeletedAsync(movieId); if (checkIfExists != null) { return($"You already have '{movie.Name}' in your favourites list."); } var userFavourite = new UserFavourite { ApplicationUserId = user.Id, MovieId = movieId, }; user.Favourites.Add(userFavourite); await this.usersRepository.SaveChangesAsync(); return($"You have successfully added '{movie.Name}' to your favourites list."); }
/// <summary> /// /// </summary> /// <remarks>Create new favourite for the current user</remarks> /// <param name="item"></param> /// <response code="201">UserFavourite created</response> public virtual IActionResult UsersCurrentFavouritesPostAsync(UserFavourite item) { item.User = null; // get the current user id int?id = GetCurrentUserId(); if (id != null) { bool user_exists = _context.Users.Any(a => a.Id == id); if (user_exists) { User user = _context.Users.First(a => a.Id == id); item.User = user; } } bool exists = _context.UserFavourites.Any(a => a.Id == item.Id); if (exists) { _context.UserFavourites.Update(item); // Save the changes _context.SaveChanges(); return(new ObjectResult(item)); } else { // record not found. add the record. _context.UserFavourites.Add(item); // Save the changes _context.SaveChanges(); return(new ObjectResult(item)); } }
/// <summary> /// Delete user favourites /// </summary> /// <remarks>Removes a specific user favourite</remarks> /// <param name="id">id of Favourite to delete</param> /// <response code="200">OK</response> public virtual IActionResult UsersCurrentFavouritesIdDeletePostAsync(int id) { // get the current user id int?userId = GetCurrentUserId(); if (userId != null) { bool exists = _context.UserFavourites.Where(x => x.User.Id == userId) .Any(a => a.Id == id); if (exists) { UserFavourite item = _context.UserFavourites.First(a => a.Id == id); _context.UserFavourites.Remove(item); // Save the changes _context.SaveChanges(); return(new ObjectResult(new HetsResponse(item))); } // record not found return(new ObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration)))); } // record not found return(new ObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration)))); }
/// <summary> /// Updates user favourites /// </summary> /// <remarks>Updates a favourite</remarks> /// <param name="item"></param> /// <response code="201">UserFavourite created</response> public virtual IActionResult UsersCurrentFavouritesPutAsync(UserFavourite item) { item.User = null; // get the current user id int?id = GetCurrentUserId(); if (id != null) { bool userExists = _context.Users.Any(a => a.Id == id); if (userExists) { User user = _context.Users.First(a => a.Id == id); item.User = user; } } // get favourites bool exists = _context.UserFavourites.Any(a => a.Id == item.Id); if (exists) { _context.UserFavourites.Update(item); // Save the changes _context.SaveChanges(); return(new ObjectResult(new HetsResponse(item))); } // record not found return(new ObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration)))); }
public async Task <IActionResult> PutUserFavourite(int id, UserFavourite userFavourite) { if (id != userFavourite.Id) { return(BadRequest()); } _context.Entry(userFavourite).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserFavouriteExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <UserFavourite> > PostUserFavourite(UserFavourite userFavourite) { _context.UserFavourites.Add(userFavourite); await _context.SaveChangesAsync(); return(CreatedAtAction("GetUserFavourite", new { id = userFavourite.Id }, userFavourite)); }
public async Task DeleteUserFavourite(UserFavourite userFavourite) { var response = await httpService.Delete($"{url}/{userFavourite.UserID}/{userFavourite.AdventureID}"); if (!response.Success) { throw new ApplicationException(await response.GetBody()); } }
public async Task CreateUserFavourite(UserFavourite userFavourite) { var response = await httpService.Post(url, userFavourite); if (!response.Success) { throw new ApplicationException(await response.GetBody()); } }
public static UserFavouriteViewModel ToViewModel(this UserFavourite model) { var dto = new UserFavouriteViewModel(); dto.FavouriteContextTypeId = model.FavouriteContextType.Id; dto.IsDefault = model.IsDefault; dto.Name = model.Name; dto.Value = model.Value; dto.Id = model.Id; return(dto); }
public async Task AddToFavouritesAsync(int experienceId, string userId) { var userFavourite = new UserFavourite() { UserId = userId, ExperienceId = experienceId, }; await this.userFavouriteRepository.AddAsync(userFavourite); await this.userFavouriteRepository.SaveChangesAsync(); }
public ActionResult Favourite(int tweetId) { var userId = (int)Session["userId"]; UserFavourite uF = new UserFavourite() { FavouriteTweetId = tweetId, UserWhoFavouritesId = userId }; favRepo.Add(uF); favRepo.SaveChanges(); return(Redirect("/User/Index")); }
/// <summary> /// Converts UserFavourite to UserFavouriteViewModel /// </summary> /// <param name="model"></param> /// <returns></returns> public static UserFavouriteViewModel ToViewModel(this UserFavourite model) { var dto = new UserFavouriteViewModel(); if (model != null) { dto.Type = model.Type; dto.IsDefault = model.IsDefault; dto.Name = model.Name; dto.Value = model.Value; dto.Id = model.Id; } return(dto); }
public bool RemoveFavourite(Guid aspNetUserId, Guid releaseId) { try { UserFavourite entityToRemove = _db.Query <UserFavourite>().Where(u => u.AspNetUserId == aspNetUserId && u.ReleaseId == releaseId).FirstOrDefault(); if (entityToRemove == null) { return(false); } else { _db.Remove <UserFavourite>(entityToRemove); return(true); } } catch (Exception) { return(false); } }
/// <summary> /// /// </summary> /// <remarks>Updates a favourite</remarks> /// <param name="item"></param> /// <response code="201">UserFavourite created</response> public virtual IActionResult UsersCurrentFavouritesPutAsync(UserFavourite item) { var result = ""; return(new ObjectResult(result)); }
public virtual IActionResult UserfavouritesPost([FromBody] UserFavourite item) { return(_service.UserfavouritesPostAsync(item)); }
public virtual IActionResult UserfavouritesIdPut([FromRoute] int id, [FromBody] UserFavourite item) { return(_service.UserfavouritesIdPutAsync(id, item)); }
public bool AddOrUpdateUserFavourite(UserFavourite userFavourite) { throw new NotImplementedException(); }
public virtual IActionResult UsersCurrentFavouritesPut([FromBody] UserFavourite item) { return(this._service.UsersCurrentFavouritesPutAsync(item)); }
private async Task SeedData(ApplicationDbContext context) { var user = new ApplicationUser() { FirstName = "Desislava", LastName = "Dzhogleva", UserName = "******", Email = "*****@*****.**", EmailConfirmed = true, PasswordHash = "someRandomHash", }; context.Users.Add(user); var location = new Location() { Name = "Perushtica", Lat = "53.555", Lng = "23.456", Type = WilderExperience.Data.Models.Enums.Type.City, }; context.Locations.Add(location); await context.SaveChangesAsync(); var experience = new Experience() { Title = "Test", Description = "Description of test", AuthorId = context.Users.First().Id, LocationId = context.Locations.First().Id, }; context.Experiences.Add(experience); await context.SaveChangesAsync(); var experience2 = new Experience() { Title = "Second", Description = "Description of test 2", AuthorId = context.Users.First().Id, LocationId = context.Locations.First().Id, }; context.Experiences.Add(experience2); await context.SaveChangesAsync(); var userFavourites = new UserFavourite() { UserId = context.Users.First().Id, ExperienceId = context.Experiences.First().Id, }; context.UsersFavourites.Add(userFavourites); await context.SaveChangesAsync(); var userFavourites2 = new UserFavourite() { UserId = context.Users.First().Id, ExperienceId = context.Experiences.Skip(1).First().Id, }; context.UsersFavourites.Add(userFavourites2); await context.SaveChangesAsync(); }
/// <summary> /// /// </summary> /// <param name="id">id of UserFavourite to fetch</param> /// <param name="item"></param> /// <response code="200">OK</response> /// <response code="404">UserFavourite not found</response> public virtual IActionResult UserfavouritesIdPutAsync(int id, UserFavourite item) { var result = ""; return(new ObjectResult(result)); }
/// <summary> /// Integration test for Current User Favourites /// </summary> public async void TestUsersCurrentFavourites() { // add a favourite. string testType = "test"; string testName = "name"; string testValue = "value"; string newValue = "newValue"; UserFavourite userFavourite = new UserFavourite(); userFavourite.Type = testType; userFavourite.Name = testName; userFavourite.Value = testValue; var request = new HttpRequestMessage(HttpMethod.Post, "/api/users/current/favourites"); request.Content = new StringContent(userFavourite.ToJson(), Encoding.UTF8, "application/json"); var response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); // parse as JSON. string jsonString = await response.Content.ReadAsStringAsync(); userFavourite = JsonConvert.DeserializeObject <UserFavourite>(jsonString); // test an update. userFavourite.Value = newValue; request = new HttpRequestMessage(HttpMethod.Put, "/api/users/current/favourites"); request.Content = new StringContent(userFavourite.ToJson(), Encoding.UTF8, "application/json"); response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); // get test results. request = new HttpRequestMessage(HttpMethod.Get, "/api/users/current/favourites/test"); response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); jsonString = await response.Content.ReadAsStringAsync(); UserFavourite[] results = JsonConvert.DeserializeObject <UserFavourite[]>(jsonString); bool found = false; foreach (UserFavourite item in results) { if (item.Value == newValue) { found = true; } } Assert.Equal(found, true); // cleanup. foreach (UserFavourite item in results) { request = new HttpRequestMessage(HttpMethod.Post, "/api/users/current/favourites/" + item.Id + "/delete"); response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); } // confirm the cleanup worked. request = new HttpRequestMessage(HttpMethod.Get, "/api/users/current/favourites/test"); response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); jsonString = await response.Content.ReadAsStringAsync(); results = JsonConvert.DeserializeObject <UserFavourite[]>(jsonString); found = false; foreach (UserFavourite item in results) { if (item.Value == newValue) { found = true; } } Assert.Equal(found, false); }
/// <summary> /// Integration test for User Favourites. /// </summary> public async void TestUserFavorites() { string initialName = "InitialName"; // create a user. var request = new HttpRequestMessage(HttpMethod.Post, "/api/users"); User user = new User(); user.GivenName = initialName; string jsonString = user.ToJson(); request.Content = new StringContent(jsonString, Encoding.UTF8, "application/json"); var response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); // parse as JSON. jsonString = await response.Content.ReadAsStringAsync(); user = JsonConvert.DeserializeObject <User>(jsonString); // get the id var id = user.Id; // add and associate the favourite request = new HttpRequestMessage(HttpMethod.Post, "/api/users/" + id + "/favourites"); UserFavourite userFavourite = new UserFavourite(); userFavourite.User = user; UserFavourite[] items = new UserFavourite[1]; items[0] = userFavourite; jsonString = JsonConvert.SerializeObject(items, Formatting.Indented); request.Content = new StringContent(jsonString, Encoding.UTF8, "application/json"); response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); // verify the user has a favourite. request = new HttpRequestMessage(HttpMethod.Get, "/api/users/" + id + "/favourites"); response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); // parse as JSON. jsonString = await response.Content.ReadAsStringAsync(); items = JsonConvert.DeserializeObject <UserFavourite[]>(jsonString); bool found = false; foreach (UserFavourite item in items) { if (item != null) { found = true; } } Assert.Equal(found, true); // remove the favourites from the user items = new UserFavourite[0]; request = new HttpRequestMessage(HttpMethod.Put, "/api/users/" + id + "/favourites"); jsonString = JsonConvert.SerializeObject(items, Formatting.Indented); request.Content = new StringContent(jsonString, Encoding.UTF8, "application/json"); response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); // verify the group membership - should be false now. request = new HttpRequestMessage(HttpMethod.Get, "/api/users/" + id + "/favourites"); response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); jsonString = await response.Content.ReadAsStringAsync(); items = JsonConvert.DeserializeObject <UserFavourite[]>(jsonString); found = false; foreach (UserFavourite item in items) { if (item != null) { found = true; } } Assert.Equal(found, false); // delete the user request = new HttpRequestMessage(HttpMethod.Post, "/api/users/" + id + "/delete"); response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); // should get a 404 if we try a get now. request = new HttpRequestMessage(HttpMethod.Get, "/api/users/" + id); response = await _client.SendAsync(request); Assert.Equal(response.StatusCode, HttpStatusCode.NotFound); }
public void Remove(UserFavourite entity) { context.UserFavourites.Remove(entity); }
public void Add(UserFavourite entity) { context.UserFavourites.Add(entity); }
/// <summary> /// Setup the test. /// </summary> public UserFavouriteModelTests() { instance = new UserFavourite(); }