-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrderHandler.cs
88 lines (74 loc) · 2.27 KB
/
OrderHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using MongoDB.Driver;
public class OrderHandler
{
public void ApplyDiscount(string orderId, string couponCode)
{
var client = new MongoClient("mongodb://localhost:27017");
var database = client.GetDatabase("orderdb");
var orders = database.GetCollection<Order>("orders");
var order = orders.Find(o => o.Id == orderId).FirstOrDefault();
if (order == null)
{
Console.WriteLine("Order not found!");
return;
}
var dayOfWeek = DateTime.Now.DayOfWeek;
if (dayOfWeek == DayOfWeek.Monday && order.User.IsPremiumMember)
{
order.Discount = 0.1;
}
if (couponCode.StartsWith("SUMMER"))
{
foreach (var item in order.Items)
{
if (item.Product.Category == "Swimwear")
{
item.Price -= item.Price * 0.5;
}
}
}
var coupons = database.GetCollection<Coupon>("coupons");
var coupon = coupons.Find(c => c.Code == couponCode).FirstOrDefault();
if (coupon != null && coupon.IsValid)
{
order.Discount += coupon.DiscountPercentage;
}
double discountedPrice = 0.0;
foreach (var item in order.Items)
{
discountedPrice += item.Price * (1 - order.Discount);
}
order.TotalPrice = discountedPrice;
Console.WriteLine("Order processed with discount");
var filter = Builders<Order>.Filter.Eq("_id", order.Id);
var update = Builders<Order>.Update.Set("TotalPrice", order.TotalPrice);
orders.UpdateOne(filter, update);
}
}
public class Order
{
public string Id { get; set; }
public User User { get; set; }
public List<Item> Items { get; set; }
public double TotalPrice { get; set; }
public double Discount { get; set; }
}
public class User
{
public bool IsPremiumMember { get; set; }
}
public class Item
{
public Product Product { get; set; }
public double Price { get; set; }
}
public class Product
{
public string Category { get; set; }
}
public class Coupon
{
public string Code { get; set; }
public double DiscountPercentage { get; set; }
public bool IsValid { get; set; }
}