Commit af1cdb99 authored by Ali Saeed's avatar Ali Saeed

init rest controller for users service

parent 84780d4a
...@@ -2,10 +2,12 @@ package org.example; ...@@ -2,10 +2,12 @@ package org.example;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication @SpringBootApplication
public class InvoicingServiceApplication { public class InvoicingServiceApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(InvoicingServiceApplication.class, args); SpringApplication.run(InvoicingServiceApplication.class, args);
} }
} }
package org.example.models;
public class Invoice {
private String invoiceID;
private String userID;
private String description;
private boolean status;
private double value;
public Invoice(String invoiceID, String userID, String description, boolean status, double value) {
this.invoiceID = invoiceID;
this.userID = userID;
this.description = description;
this.status = status;
this.value = value;
}
public String getInvoiceID() {
return invoiceID;
}
public void setInvoiceID(String invoiceID) {
this.invoiceID = invoiceID;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
@Override
public String toString() {
return "Invoice{" +
"invoiceID='" + invoiceID + '\'' +
", userID='" + userID + '\'' +
", description='" + description + '\'' +
", status=" + status +
", value=" + value +
'}';
}
}
package org.example.models;
public class User {
private String userId;
private String name;
private double balance;
public User(String userId, String name, double balance) {
this.userId = userId;
this.name = name;
this.balance = balance;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
@Override
public String toString() {
return "User{" +
"userId='" + userId + '\'' +
", name='" + name + '\'' +
", balance=" + balance +
'}';
}
}
package org.example.resources;
import org.example.models.Invoice;
import org.example.models.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserResource {
@RequestMapping("/{userId}")
public List<Invoice> getUnPaidInvoices(@PathVariable("userId") String userId){
Invoice inv1 = new Invoice("1", "1","ssssssd",true,55.0);
Invoice inv2 = new Invoice("2", "1","dddddddddd",false,545.0);
System.out.println(userId);
ArrayList<Invoice> inv = new ArrayList<Invoice>();
inv.add(inv1);
inv.add(inv2);
return inv;
}
@RequestMapping("/{userId}/{invoiceId}")
public boolean payInvoice(@PathVariable("userId") String userId,@PathVariable("invoiceId") String invoiceId){
User user = new User("1","Ali",500.0);
Invoice invoice = new Invoice("2", "1","dddddddddd",false,545.0);
if(user.getBalance() >= invoice.getValue()){
user.setBalance(user.getBalance() - invoice.getValue());
invoice.setStatus(true);
return true;
}
else{
System.out.println("you do not have enough balance!!!");
return false;
}
}
@RequestMapping()
public HashMap<User,Invoice> getAllUsersHaveUnPaidInvoices(){
User user1 = new User("1","Ali",500.0);
User user2 = new User("2","Hasan",5000.0);
Invoice invoice1 = new Invoice("2", "1","dddddddddd",false,545.0);
Invoice invoice2 = new Invoice("3", "2","dddddddddd",false,545.0);
ArrayList<Invoice> inv = new ArrayList<Invoice>();
inv.add(invoice1);
inv.add(invoice2);
HashMap<User,Invoice> userInvoiceHashMap = new HashMap<>();
for (Invoice invoice: inv)
{
String id = invoice.getUserID();
}
userInvoiceHashMap.put(user1,invoice1);
userInvoiceHashMap.put(user2,invoice2);
return userInvoiceHashMap;
}
@RequestMapping()
public HashMap<User,Invoice> getAllUsersHavePaidInvoices(){
User user1 = new User("1","Ali",500.0);
User user2 = new User("2","Hasan",5000.0);
Invoice invoice1 = new Invoice("2", "1","dddddddddd",true,545.0);
Invoice invoice2 = new Invoice("3", "2","dddddddddd",true,545.0);
ArrayList<Invoice> inv = new ArrayList<Invoice>();
inv.add(invoice1);
inv.add(invoice2);
HashMap<User,Invoice> userInvoiceHashMap = new HashMap<>();
for (Invoice invoice: inv)
{
String id = invoice.getUserID();
}
userInvoiceHashMap.put(user1,invoice1);
userInvoiceHashMap.put(user2,invoice2);
return userInvoiceHashMap;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment