public async Task <IActionResult> Put(PostApiModel apiModel) { var user = await _userRepo.GetUserByIdAsync(apiModel.User.Id); var email = User.FindFirst(ct => ct.Type.Contains("nameidentifier")).Value; if (email.ToLower() == user.Email.ToLower()) { try { apiModel.User = ApiModelConverter.ToUserApiModel(user); var post = ApiModelConverter.ToPost(_userRepo, _commentRepo, apiModel); await _postRepo.UpdatePostAsync(post); return(Ok()); } catch (ArgumentException ex) { return(BadRequest(ex.Message)); } catch { return(BadRequest()); } } else { return(BadRequest()); } }
public async Task <IActionResult> Post(CommentApiModel apiModel) { try { var email = User.FindFirst(ct => ct.Type.Contains("nameidentifier")).Value; var user = await _userRepo.GetUserByEmailAsync(email); apiModel.User = ApiModelConverter.ToUserApiModel(user); Comment comment = ApiModelConverter.ToComment(_commentRepo, _userRepo, _postRepo, apiModel); int result = await _commentRepo.CreateAsync(comment); if (result != -1) { return(Ok()); } else { throw new ArgumentException("Could not create comment"); } } catch (ArgumentException ex) { return(BadRequest(ex.Message)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public void ConvertOrderItemsAndCustomerInfoToOrderDeliveryModel_EmptyOrderList_ReturnsValidModel() { // Arrange var apiModelConverter = new ApiModelConverter(); var orderItems = new List <OrderItem>(); var customerDetail = new CustomerDetailDto { CustomerId = "ABC123", Email = "*****@*****.**", FirstName = "Mr", LastName = "Blue", LastLoggedIn = DateTime.UtcNow.ToString(), HouseNumber = "1", Street = "Street", Town = "Big Town", Postcode = "PO5T C0D3" }; // Act var result = apiModelConverter.ConvertOrderItemsAndCustomerInfoToOrderDeliveryModel(orderItems, customerDetail); // Assert Assert.AreEqual("Mr", result.Customer.FirstName); Assert.AreEqual("Blue", result.Customer.LastName); Assert.IsNull(result.Order); }
public async Task <IActionResult> Put(UserApiModel apiModel, int id = -1) { var user = await _userRepo.GetUserByIdAsync(apiModel.Id); var email = User.FindFirst(ct => ct.Type.Contains("nameidentifier")).Value; // if the id is null switch to bad request if (email.ToLower() == user.Email.ToLower()) { try { if (id == -1) { throw new ArgumentException("id cannot be -1"); } var result = ApiModelConverter.ToUser(_userRepo, apiModel); await _userRepo.UpdateUserAsync(id, result); return(Ok()); } catch (ArgumentException ex) { return(BadRequest(ex.Message)); } catch { return(BadRequest()); } } else { return(BadRequest()); } }
public async Task <IActionResult> Get() { try { var email = User.FindFirst(ct => ct.Type.Contains("nameidentifier")).Value; var user = await _userRepo.GetUserByEmailAsync(email); if (user != null) { var posts = await _postRepo.GetFollowingPosts(user.Id); return(Ok(posts.Select(p => ApiModelConverter.ToPostApiModel(p)))); } else { return(StatusCode(StatusCodes.Status403Forbidden)); } } catch (ArgumentException ex) { return(BadRequest(ex.Message)); } catch { return(BadRequest()); } }
public async Task <ActionResult <List <PostApiModel> > > GetUserPosts(int id) { List <Post> posts = await _postRepo.GetPostsByUserIdAsync(id); return(posts .Select(p => ApiModelConverter.ToPostApiModel(p)) .ToList()); }
public void ConvertOrderItemsAndCustomerInfoToOrderDeliveryModel_InvalidCustomerDetailDto() { // Arrange var apiModelConverter = new ApiModelConverter(); var orderItems = new List <OrderItem>(); CustomerDetailDto customerDetail = null; // Assert Assert.ThrowsException <ArgumentNullException>(() => apiModelConverter.ConvertOrderItemsAndCustomerInfoToOrderDeliveryModel(orderItems, customerDetail)); }
public async Task <ActionResult <List <PostApiModel> > > GetUserPosts() { var email = User.FindFirst(ct => ct.Type.Contains("nameidentifier")).Value; var user = await _userRepo.GetUserByEmailAsync(email); List <Post> posts = await _postRepo.GetPostsByUserIdAsync(user.Id); return(posts .Select(p => ApiModelConverter.ToPostApiModel(p)) .ToList()); }
public void ConvertOrderItemsAndCustomerInfoToOrderDeliveryModel_DatesAreFormattedCorrectly() { // Arrange var apiModelConverter = new ApiModelConverter(); var orderItems = new List <OrderItem>() { new OrderItem() { Order = new Order() { OrderId = 1, DeliveryExpected = DateTime.Today, OrderDate = DateTime.Today }, Price = 1, Quantity = 1, Product = new Product() { ProductName = "Best product" } } }; var customerDetail = new CustomerDetailDto { CustomerId = "ABC123", Email = "*****@*****.**", FirstName = "Mr", LastName = "Blue", LastLoggedIn = DateTime.UtcNow.ToString(), HouseNumber = "1", Street = "Street", Town = "Big Town", Postcode = "PO5T C0D3" }; // Act var result = apiModelConverter.ConvertOrderItemsAndCustomerInfoToOrderDeliveryModel(orderItems, customerDetail); // Assert Assert.AreEqual("Mr", result.Customer.FirstName); Assert.AreEqual("Blue", result.Customer.LastName); Assert.AreEqual(DateTime.Today.ToString("dd-MMM-yyyy"), result.Order.DeliveryExpected); Assert.AreEqual(DateTime.Today.ToString("dd-MMM-yyyy"), result.Order.OrderDate); }
public void ConvertOrderItemsAndCustomerInfoToOrderDeliveryModel_OrderIsAGift_ReturnsProductNameAsGift() { // Arrange var apiModelConverter = new ApiModelConverter(); var orderItems = new List <OrderItem>() { new OrderItem() { Order = new Order() { OrderId = 1, DeliveryExpected = DateTime.Today, OrderDate = DateTime.Today, ContainsGift = true }, Price = 1, Quantity = 1, Product = new Product() { ProductName = "Best product" } } }; var customerDetail = new CustomerDetailDto { CustomerId = "ABC123", Email = "*****@*****.**", FirstName = "Mr", LastName = "Blue", LastLoggedIn = DateTime.UtcNow.ToString(), HouseNumber = "1", Street = "Street", Town = "Big Town", Postcode = "PO5T C0D3" }; // Act var result = apiModelConverter.ConvertOrderItemsAndCustomerInfoToOrderDeliveryModel(orderItems, customerDetail); // Assert Assert.AreEqual("Mr", result.Customer.FirstName); Assert.AreEqual("Blue", result.Customer.LastName); Assert.IsTrue(result.Order.OrderItems.All(x => x.Product == "Gift")); }
public async Task <IActionResult> Post(UserApiModel apiModel) { try { var user = ApiModelConverter.ToUser(_userRepo, apiModel); int result = await _userRepo.CreateUser(user); if (result != -1) { // return Created(); return(Ok()); } else { return(BadRequest()); } } catch (ArgumentException ex) { return(BadRequest(ex.Message)); } }
public async Task <IActionResult> Get(int id) { var post = await _postRepo.GetPostByIdAsync(id); return(Ok(ApiModelConverter.ToPostApiModel(post))); }
public async Task <IActionResult> Get() { var posts = await _postRepo.GetAllPostsAsync(); return(Ok(posts.Select(p => ApiModelConverter.ToPostApiModel(p)))); }