/// <summary> /// Dodaje pracownika do bazy danych /// </summary> /// <param name="newEmployee">Nowy pracownik</param> public IHttpActionResult Post(EmployeeAddDto newEmployee) { if (!ModelState.IsValid) { return(BadRequest()); } Employee employee = Mapper.Map <Employee>(newEmployee); var password = RandomString(15); var role = newEmployee.Role; var user = new User { UserName = GenerateUserName(employee) }; if (context.Users.Any(u => user.UserName == u.UserName)) { return(BadRequest()); } var result = userManager.Create(user, password); if (!result.Succeeded) { return(BadRequest(result.Errors.ToString())); } userManager.AddToRole(user.Id, role.ToString()); employee.User = user; context.Employees.Add(employee); context.SaveChanges(); using (var eh = new EMailHelper()) { eh.SendEmail(employee.Email, "Instant Delivery - Rejestracja", eh.RegistrationBody(employee, password)); } return(Ok(employee.Id)); }
public IHttpActionResult Register(CustomerRegisterDto newCustomer) { if (newCustomer == null || !ModelState.IsValid) { return(BadRequest(ModelState)); } Customer customer = Mapper.Map <Customer>(newCustomer); var user = new User { UserName = newCustomer.UserName }; if (context.Users.Any(u => user.UserName == u.UserName)) { return(BadRequest("Ta nazwa użytkownika jest już zajęta")); } context.Customers.Add(customer); customer.User = user; var result = userManager.Create(user, newCustomer.Password); if (!result.Succeeded) { return(BadRequest(result.Errors.ToString())); } userManager.AddToRole(user.Id, Role.Customer.ToString()); context.SaveChanges(); return(Ok()); }
/// <summary> /// Dodaje pojazd do bazy danych. /// </summary> /// <param name="newVehicle"></param> /// <returns></returns> public IHttpActionResult Post(AddVehicleDto newVehicle) { if (!ModelState.IsValid) { return(BadRequest()); } var model = context.VehicleModels.Find(newVehicle.VehicleModelId); var vehicle = Mapper.Map <Vehicle>(newVehicle); vehicle.VehicleModel = model; context.Vehicles.Add(vehicle); context.SaveChanges(); return(Ok()); }
public IHttpActionResult RegisterPackage(PackageDto package) { if (!ModelState.IsValid) { return(BadRequest()); } package.Status = PackageStatus.InWarehouse; package.Cost = pricingStrategy.GetCost(package); var newPackage = Mapper.Map <Package>(package); context.Packages.Add(newPackage); context.PackageEvents.Add(new PackageEvent { EventType = PackageEventType.RegisteredInWarehouse, Package = newPackage }); context.SaveChanges(); return(Ok()); }