/// <summary> Parses one command and makes action or shows view. </summary> /// <param name="line">One command as string.</param> /// <exception cref="System.Exception">Throws when command is not correct by definition.</exception> internal void ProcessCommand(string line) { string[] tokens = line.Split(' '); if (tokens.Length != 3) { throw new Exception("Command length wrong."); } if (tokens[0] != "GET") { throw new Exception("Not a GET request."); } if (tokens[2].Substring(0, domain.Length) != domain) { throw new Exception("Wrong domain."); } Customer customer; if ((customer = model.GetCustomer(int.Parse(tokens[1]))) == null) { throw new Exception("Customer doesn't exists."); } string[] cmds = tokens[2]. Substring(domain.Length). Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); if (cmds.Length == 0) { throw new Exception("HTTP address is too short."); } switch (cmds[0]) { case "Books": if (cmds.Length == 1) { view.ShowBooks(model.GetBooks(), customer); } else if (cmds.Length == 3 && cmds[1] == "Detail") { Book detail = model.GetBook(int.Parse(cmds[2])); if (detail == null) { throw new Exception("Book Detail not found."); } view.ShowBookDetail(detail, customer); } else { throw new Exception("Books HTTP address is wrong."); } break; case "ShoppingCart": if (cmds.Length == 1) { view.ShowShoppingCart(customer, model); } else if (cmds.Length == 3) { switch (cmds[1]) { case "Add": AddBook(customer, model.GetBook(int.Parse(cmds[2]))); break; case "Remove": RemoveBook(customer, model.GetBook(int.Parse(cmds[2]))); break; default: throw new Exception("ShoppingCart HTTP address is wrong."); } } else { throw new Exception("ShoppingCart HTTP address is wrong."); } break; default: throw new Exception("HTTP address is wrong."); } }
public static ModelStore LoadFrom(TextReader reader) { var store = new ModelStore(); try { if (reader.ReadLine() != "DATA-BEGIN") { return(null); } while (true) { string line = reader.ReadLine(); if (line == null) { return(null); } else if (line == "DATA-END") { break; } string[] tokens = line.Split(';'); switch (tokens[0]) { case "BOOK": store.books.Add(new Book { Id = int.Parse(tokens[1]), Title = tokens[2], Author = tokens[3], Price = decimal.Parse(tokens[4]) }); break; case "CUSTOMER": store.customers.Add(new Customer { Id = int.Parse(tokens[1]), FirstName = tokens[2], LastName = tokens[3] }); break; case "CART-ITEM": var customer = store.GetCustomer(int.Parse(tokens[1])); if (customer == null) { return(null); } customer.ShoppingCart.Items.Add(new ShoppingCartItem { BookId = int.Parse(tokens[2]), Count = int.Parse(tokens[3]) }); break; default: return(null); } } } catch (Exception ex) { if (ex is FormatException || ex is IndexOutOfRangeException) { return(null); } throw; } return(store); }
public static ModelStore LoadFrom(TextReader reader) { var store = new ModelStore(); try { if(reader.ReadLine() != "DATA-BEGIN") { return null; } while(true) { string line = reader.ReadLine(); if(line == null) { return null; } else if(line == "DATA-END") { break; } string[] tokens = line.Split(';'); switch(tokens[0]) { case "BOOK": store.books.Add(new Book { Id = int.Parse(tokens[1]), Title = tokens[2], Author = tokens[3], Price = decimal.Parse(tokens[4]) }); break; case "CUSTOMER": store.customers.Add(new Customer { Id = int.Parse(tokens[1]), FirstName = tokens[2], LastName = tokens[3] }); break; case "CART-ITEM": var customer = store.GetCustomer(int.Parse(tokens[1])); if(customer == null) { return null; } customer.ShoppingCart.Items.Add(new ShoppingCartItem { BookId = int.Parse(tokens[2]), Count = int.Parse(tokens[3]) }); break; default: return null; } } } catch(Exception ex) { if(ex is FormatException || ex is IndexOutOfRangeException) { return null; } throw; } return store; }