diff --git a/bookstore-api/.idea/workspace.xml b/bookstore-api/.idea/workspace.xml index 1114214..ea61eed 100644 --- a/bookstore-api/.idea/workspace.xml +++ b/bookstore-api/.idea/workspace.xml @@ -16,11 +16,11 @@ - + - - + + @@ -28,6 +28,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -91,7 +133,6 @@ @@ -914,12 +957,12 @@ - + - @@ -942,7 +985,7 @@ - + @@ -975,28 +1018,6 @@ - - - - - - - - - - - - - - - - - - - - - - @@ -1109,13 +1130,6 @@ - - - - - - - @@ -1312,15 +1326,49 @@ + + + + + + + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/bookstore-api/src/main/java/com/bookstore/domain/UserShipping.java b/bookstore-api/src/main/java/com/bookstore/domain/UserShipping.java index 8daf062..26968f8 100644 --- a/bookstore-api/src/main/java/com/bookstore/domain/UserShipping.java +++ b/bookstore-api/src/main/java/com/bookstore/domain/UserShipping.java @@ -1,5 +1,7 @@ package com.bookstore.domain; +import com.fasterxml.jackson.annotation.JsonIgnore; + import javax.persistence.*; import java.io.Serializable; @@ -25,6 +27,7 @@ public class UserShipping implements Serializable { @ManyToOne @JoinColumn(name = "user_id") + @JsonIgnore private User user; public Long getId() { diff --git a/bookstore-api/src/main/java/com/bookstore/resource/PaymentResource.java b/bookstore-api/src/main/java/com/bookstore/resource/PaymentResource.java index 281dbdc..7e59f6b 100644 --- a/bookstore-api/src/main/java/com/bookstore/resource/PaymentResource.java +++ b/bookstore-api/src/main/java/com/bookstore/resource/PaymentResource.java @@ -45,39 +45,39 @@ public class PaymentResource { return new ResponseEntity("Payment Added(Updated) Successfully!", HttpStatus.OK); } - @RequestMapping("/update") - public ResponseEntity updateCreditCard( - @RequestBody String id, Principal principal, - Model model - ) { - - User user = userService.findByUsername(principal.getName()); - UserPayment userPayment = userPaymentService.findById(Long.parseLong(id)); - - if (user.getId()!=userPayment.getUser().getId()) { - return new ResponseEntity("Invalid Request", HttpStatus.BAD_REQUEST); - } else { - - model.addAttribute("user", user); - UserBilling userBilling = userPayment.getUserBilling(); - model.addAttribute("userPayment", userPayment); - model.addAttribute("userBilling", userBilling); - - List stateList = USConstants.listOfUSStatesCode; - Collections.sort(stateList); - model.addAttribute("stateList", stateList); - - model.addAttribute("addNewCreditCard", true); - model.addAttribute("classActiveBilling", true); - model.addAttribute("listOfShippingAddresses", true); - model.addAttribute("userPaymentList", user.getUserPaymentList()); - model.addAttribute("userShippingList", user.getUserShippingList()); - model.addAttribute("orderList", user.getOrderList()); - - - return new ResponseEntity("Payment Added(Updated) Successfully!", HttpStatus.OK); - } - } +// @RequestMapping("/update") +// public ResponseEntity updateCreditCard( +// @RequestBody String id, Principal principal, +// Model model +// ) { +// +// User user = userService.findByUsername(principal.getName()); +// UserPayment userPayment = userPaymentService.findById(Long.parseLong(id)); +// +// if (user.getId()!=userPayment.getUser().getId()) { +// return new ResponseEntity("Invalid Request", HttpStatus.BAD_REQUEST); +// } else { +// +// model.addAttribute("user", user); +// UserBilling userBilling = userPayment.getUserBilling(); +// model.addAttribute("userPayment", userPayment); +// model.addAttribute("userBilling", userBilling); +// +// List stateList = USConstants.listOfUSStatesCode; +// Collections.sort(stateList); +// model.addAttribute("stateList", stateList); +// +// model.addAttribute("addNewCreditCard", true); +// model.addAttribute("classActiveBilling", true); +// model.addAttribute("listOfShippingAddresses", true); +// model.addAttribute("userPaymentList", user.getUserPaymentList()); +// model.addAttribute("userShippingList", user.getUserShippingList()); +// model.addAttribute("orderList", user.getOrderList()); +// +// +// return new ResponseEntity("Payment Added(Updated) Successfully!", HttpStatus.OK); +// } +// } @RequestMapping(value = "/remove", method = RequestMethod.POST) public ResponseEntity removePaymentPost( diff --git a/bookstore-api/src/main/java/com/bookstore/resource/ShippingResource.java b/bookstore-api/src/main/java/com/bookstore/resource/ShippingResource.java new file mode 100644 index 0000000..86799b2 --- /dev/null +++ b/bookstore-api/src/main/java/com/bookstore/resource/ShippingResource.java @@ -0,0 +1,70 @@ +package com.bookstore.resource; + +import com.bookstore.domain.User; +import com.bookstore.domain.UserBilling; +import com.bookstore.domain.UserPayment; +import com.bookstore.domain.UserShipping; +import com.bookstore.service.ShippingAddressService; +import com.bookstore.service.UserPaymentService; +import com.bookstore.service.UserService; +import com.bookstore.service.UserShippingService; +import com.bookstore.utility.USConstants; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import java.security.Principal; +import java.util.Collections; +import java.util.List; + +/** + * Created by z00382545 on 3/9/17. + */ + +@RestController +@RequestMapping("/shipping") +public class ShippingResource { + @Autowired + private UserService userService; + + @Autowired + private UserShippingService userShippingService; + + @RequestMapping(value = "/add", method = RequestMethod.POST) + public ResponseEntity AddNewUserShippingPost( + @RequestBody UserShipping userShipping, + Principal principal, Model model) { + User user = userService.findByUsername(principal.getName()); + + + userService.updateUserShipping(userShipping, user); + + return new ResponseEntity("Shipping Added(Updated) Successfully!", HttpStatus.OK); + } + + @RequestMapping(value = "/remove", method = RequestMethod.POST) + public ResponseEntity removeUserShippingPost( + @RequestBody String id, + Principal principal, Model model) { + User user = userService.findByUsername(principal.getName()); + + userShippingService.removeById(Long.parseLong(id)); + + return new ResponseEntity("Shipping Removed Successfully!", HttpStatus.OK); + } + + @RequestMapping(value = "/setDefault", method = RequestMethod.POST) + public ResponseEntity setDefaultUserShippingPost( + @RequestBody String id, + Principal principal, Model model) { + User user = userService.findByUsername(principal.getName()); + userService.setUserDefaultShipping(Long.parseLong(id), user); + + return new ResponseEntity("Set Default Shipping Successfully!", HttpStatus.OK); + } +} diff --git a/bookstore-api/target/classes/com/bookstore/domain/UserShipping.class b/bookstore-api/target/classes/com/bookstore/domain/UserShipping.class index d0aae99..06794c7 100644 Binary files a/bookstore-api/target/classes/com/bookstore/domain/UserShipping.class and b/bookstore-api/target/classes/com/bookstore/domain/UserShipping.class differ diff --git a/bookstore-api/target/classes/com/bookstore/resource/PaymentResource.class b/bookstore-api/target/classes/com/bookstore/resource/PaymentResource.class index db7f538..c40da50 100644 Binary files a/bookstore-api/target/classes/com/bookstore/resource/PaymentResource.class and b/bookstore-api/target/classes/com/bookstore/resource/PaymentResource.class differ diff --git a/bookstore-api/target/classes/com/bookstore/resource/ShippingResource.class b/bookstore-api/target/classes/com/bookstore/resource/ShippingResource.class new file mode 100644 index 0000000..5ef163d Binary files /dev/null and b/bookstore-api/target/classes/com/bookstore/resource/ShippingResource.class differ diff --git a/store-front/src/app/app.module.ts b/store-front/src/app/app.module.ts index c7f4ec2..7c9ad77 100644 --- a/store-front/src/app/app.module.ts +++ b/store-front/src/app/app.module.ts @@ -14,6 +14,7 @@ import { NavBarComponent } from './components/nav-bar/nav-bar.component'; import { LoginService } from './services/login.service'; import { UserService } from './services/user.service'; import { PaymentService } from './services/payment.service'; +import { ShippingService } from './services/shipping.service'; import { MyAccountComponent } from './components/my-account/my-account.component'; import { MyProfileComponent } from './components/my-profile/my-profile.component'; @@ -35,7 +36,8 @@ import { MyProfileComponent } from './components/my-profile/my-profile.component providers: [ LoginService, UserService, - PaymentService + PaymentService, + ShippingService ], bootstrap: [AppComponent] }) diff --git a/store-front/src/app/components/my-profile/my-profile.component.html b/store-front/src/app/components/my-profile/my-profile.component.html index 256a647..a270abc 100644 --- a/store-front/src/app/components/my-profile/my-profile.component.html +++ b/store-front/src/app/components/my-profile/my-profile.component.html @@ -74,7 +74,7 @@

Default payment set successfully!

- +
@@ -252,7 +252,7 @@

Default shipping address set successfully!

- +
@@ -269,7 +269,7 @@ default - +
Shipping Address{{userShipping.userShippingStreet1}}, {{userShipping.userShippingCity}}, {{userShipping.userShippingState}} @@ -283,7 +283,7 @@ -
+
@@ -325,7 +325,7 @@

- +
diff --git a/store-front/src/app/components/my-profile/my-profile.component.ts b/store-front/src/app/components/my-profile/my-profile.component.ts index 25b1e51..cc15112 100644 --- a/store-front/src/app/components/my-profile/my-profile.component.ts +++ b/store-front/src/app/components/my-profile/my-profile.component.ts @@ -4,6 +4,7 @@ import {Router} from "@angular/router"; import {LoginService} from "../../services/login.service"; import {UserService} from "../../services/user.service"; import {PaymentService} from "../../services/payment.service"; +import {ShippingService} from "../../services/shipping.service"; import {User} from '../../models/user'; import {UserPayment} from '../../models/user-payment'; import {UserBilling} from '../../models/user-billing'; @@ -35,19 +36,34 @@ export class MyProfileComponent implements OnInit { private userPayment: UserPayment = new UserPayment(); private userBilling: UserBilling = new UserBilling(); private userPaymentList: UserPayment[] = []; - private stateList: string[] = []; private userShipping: UserShipping = new UserShipping(); + private userShippingList: UserShipping[] = []; + private stateList: string[] = []; private selectedProfileTab:number = 0; private selectedBillingTab:number = 0; + private selectedShippingTab:number = 0; private defaultUserPaymentId:number; private defaultPaymentSet:boolean; - constructor (private paymentService:PaymentService, private loginService: LoginService, private userService: UserService, private router: Router){ + private defaultUserShippingId:number; + private defaultShippingSet: boolean; + + constructor ( + private paymentService:PaymentService, + private shippingService:ShippingService, + private loginService: LoginService, + private userService: UserService, + private router: Router + ){ } - selectedIndexChange(val :number ){ + selectedShippingChange(val :number ){ + this.selectedShippingTab=val; + } + + selectedBillingChange(val :number ){ this.selectedBillingTab=val; } @@ -118,7 +134,6 @@ export class MyProfileComponent implements OnInit { onUpdatePayment(payment:UserPayment) { this.userPayment = payment; this.userBilling = payment.userBilling; - this.selectedBillingTab=1; } onRemovePayment(id:number) { @@ -148,16 +163,68 @@ export class MyProfileComponent implements OnInit { ); } + onNewShipping () { + this.shippingService.newShipping(this.userShipping).subscribe( + res => { + this.getCurrentUser(); + this.selectedShippingTab = 0; + }, + error => { + console.log(error.text()); + } + ); + } + + onUpdateShipping(shipping:UserShipping) { + this.userShipping = shipping; + this.selectedShippingTab=1; + } + + onRemoveShipping(id:number) { + this.shippingService.removeShipping(id).subscribe( + res => { + this.getCurrentUser(); + + }, + error => { + console.log(error.text()); + } + ); + } + + setDefaultShipping() { + this.defaultShippingSet=false; + this.shippingService.setDefaultShipping(this.defaultUserShippingId).subscribe( + res => { + this.getCurrentUser(); + this.defaultShippingSet=true; + // this.selectedProfileTab = 2; + // this.selectedBillingTab = 0; + }, + error => { + console.log(error.text()); + } + ); + } + getCurrentUser() { this.userService.getCurrentUser().subscribe( res => { this.user=res.json(); this.userPaymentList = this.user.userPaymentList; + this.userShippingList = this.user.userShippingList; for (let index in this.userPaymentList) { if (this.userPaymentList[index].defaultPayment) { this.defaultUserPaymentId=this.userPaymentList[index].id; - return; + break; + } + } + + for (let i in this.userShippingList) { + if (this.userShippingList[i].userShippingDefault) { + this.defaultUserShippingId=this.userShippingList[i].id; + break; } } }, @@ -191,5 +258,7 @@ export class MyProfileComponent implements OnInit { this.defaultPaymentSet=false; this.userShipping.userShippingState=""; + this.defaultShippingSet=false; + } } diff --git a/store-front/src/app/models/user.ts b/store-front/src/app/models/user.ts index b21549d..1c8c41a 100644 --- a/store-front/src/app/models/user.ts +++ b/store-front/src/app/models/user.ts @@ -1,4 +1,5 @@ import {UserPayment} from './user-payment'; +import {UserShipping} from './user-shipping'; export class User { public id: number; @@ -10,4 +11,5 @@ export class User { public phone: string; public enabled: boolean; public userPaymentList: UserPayment[]; + public userShippingList: UserShipping[]; } diff --git a/store-front/src/app/services/shipping.service.spec.ts b/store-front/src/app/services/shipping.service.spec.ts new file mode 100644 index 0000000..d4d2fcd --- /dev/null +++ b/store-front/src/app/services/shipping.service.spec.ts @@ -0,0 +1,16 @@ +/* tslint:disable:no-unused-variable */ + +import { TestBed, async, inject } from '@angular/core/testing'; +import { ShippingService } from './shipping.service'; + +describe('ShippingService', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [ShippingService] + }); + }); + + it('should ...', inject([ShippingService], (service: ShippingService) => { + expect(service).toBeTruthy(); + })); +}); diff --git a/store-front/src/app/services/shipping.service.ts b/store-front/src/app/services/shipping.service.ts new file mode 100644 index 0000000..970b2ef --- /dev/null +++ b/store-front/src/app/services/shipping.service.ts @@ -0,0 +1,41 @@ +import { Injectable } from '@angular/core'; +import {AppConst} from '../constants/app-const'; +import {Http, Headers} from '@angular/http'; +import {UserShipping} from '../models/user-shipping'; + +@Injectable() +export class ShippingService { + private serverPath:string = AppConst.serverPath; + + constructor(private http:Http) { } + + newShipping(shipping: UserShipping) { + let url = this.serverPath+"/shipping/add"; + + let tokenHeader = new Headers ({ + 'Content-Type': 'application/json', + 'x-auth-token' : localStorage.getItem("xAuthToken") + }); + return this.http.post(url, JSON.stringify(shipping), {headers : tokenHeader}); + } + + removeShipping(id: number) { + let url = this.serverPath+"/shipping/remove"; + + let tokenHeader = new Headers ({ + 'Content-Type': 'application/json', + 'x-auth-token' : localStorage.getItem("xAuthToken") + }); + return this.http.post(url, id, {headers : tokenHeader}); + } + + setDefaultShipping(id: number) { + let url = this.serverPath+"/shipping/setDefault"; + + let tokenHeader = new Headers ({ + 'Content-Type': 'application/json', + 'x-auth-token' : localStorage.getItem("xAuthToken") + }); + return this.http.post(url, id, {headers : tokenHeader}); + } +} \ No newline at end of file