public RegistrationResponse RegisterNewCustomer(RegistrationRequest request) { try { using (var unitOfWork = new AptekaNetUnitOfWork()) { foreach (var customer in unitOfWork.Context.Customers) { Debug.WriteLine("USER: {0} , {1}", customer.Login, customer.Password); } if (!unitOfWork.CustomerRepository.IsCustomerLoginUnique(request.NewCustomerData.Login)) return new RegistrationResponse(request.Id, false) { Message = "Użytkownik o podanym loginie już istnieje" }; var customerAssembler = new CustomerAssembler(); Customer newCustomerEntity = customerAssembler.ToNewCustomerEntity(request.NewCustomerData); unitOfWork.CustomerRepository.Create(newCustomerEntity); unitOfWork.SaveChanges(); return new RegistrationResponse(request.Id, true); } } catch (DbEntityValidationException e) { var message = e.EntityValidationErrors.Aggregate("Nieprawidłowe dane użytkownika: ", (current1, errors) => errors.ValidationErrors.Aggregate(current1, (current, error) => current + (error.ErrorMessage + " "))); return new RegistrationResponse(request.Id, false) { Message = message }; } catch (Exception e) { return new RegistrationResponse(request.Id, false) { Message = "Błąd - " + e.Message }; } }
public OrderResponse PlaceOrder(OrderRequest request) { if (!_userSignedIn) return new OrderResponse(request.Id,false) { Message = "Użytkownik niezalogowany" }; if (request.ProductIdQuantityDictionary.Keys.Count==0) return new OrderResponse(request.Id, false) { Message = "Zamówienie nie może być puste" }; try { Debug.WriteLine("{0} - User {1}: Place Order - begin", DateTime.Now, _signedCustomerId); lock (_placeOrderSynchronizationObject) { using (var unitOfWork = new AptekaNetUnitOfWork()) { var customer = unitOfWork.CustomerRepository.GetCustomerById(_signedCustomerId); var orderDetails = new List<OrderDetail>(); Pharmacy pharmacy = null; //Pobranie odpowiednich produktow z bazy danych na podstawie podanych id + sprawdzenie stanu produktu w aptece foreach (var productId in request.ProductIdQuantityDictionary.Keys) { var prod = unitOfWork.ProductRepository.GetProductById(productId); var quantity = request.ProductIdQuantityDictionary[productId]; if (prod.OnStock < quantity) return new OrderResponse(request.Id, false) { Message = String.Format("W aptece nie ma wystarczającej ilości leku: {0}", prod.Medicine.CommercialName) }; else { prod.OnStock -= quantity; } orderDetails.Add(new OrderDetail { Product = prod, Quantity = quantity }); if (pharmacy != null && prod.Pharmacy != pharmacy) return new OrderResponse(request.Id, false) { Message = "Produkty nie mogą pochodzić z różnych aptek" }; pharmacy = prod.Pharmacy; } var order = new Order { Customer = customer, Date = DateTime.Now, EmployeeId = unitOfWork.EmployeeRepository.Find(e => e.Pharmacy.Id==pharmacy.Id).Id }; foreach (var orderDetail in orderDetails) { order.OrderedProducts.Add(orderDetail); } pharmacy.Orders.Add(order); unitOfWork.SaveChanges(); Debug.WriteLine("Zamówienie ID: {0}",order.Id); } } Debug.WriteLine("{0} - User {1}: Place Order - end", DateTime.Now, _signedCustomerId); return new OrderResponse(request.Id, true) { Message = "Zamówienie dodane pomyślnie" }; } catch (Exception e) { return new OrderResponse(request.Id, false) { Message = String.Format("Błąd - {0}",e.Message) }; } }