public HttpResponseMessage PostBusinessLocation(BusinessLocationDTO businessLocationDTO) { if (ClaimsAuthorization.CheckAccess("Put", "BusinessId", businessLocationDTO.BusinessId.ToString())) { if (ModelState.IsValid) { //Ensure this is not a duplicate location name var existingBusLoc = db.BusinessLocations.FirstOrDefault(bl => bl.Business.Id == businessLocationDTO.BusinessId && bl.Name == businessLocationDTO.Name); if (existingBusLoc != null) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "A business location with this name already exists.")); } var buslocation = MapperFacade.MapperConfiguration.Map <BusinessLocationDTO, BusinessLocation>(businessLocationDTO); buslocation.Id = Guid.NewGuid(); //Assign new ID on save. buslocation.Business = db.Businesses.Find(businessLocationDTO.BusinessId); buslocation.Enabled = true; foreach (var intLoc in businessLocationDTO.InternalLocations) { if (!String.IsNullOrEmpty(intLoc.Name)) { buslocation.InternalLocations.Add(new InternalLocation { Id = Guid.NewGuid(), Name = intLoc.Name, BusinessLocation = buslocation }); } } //Need to create a default preferences object BusinessPreferences busPref = new BusinessPreferences { Id = Guid.NewGuid(), CancelShiftAllowed = true, CancelShiftTimeframe = 0, BusinessLocation = buslocation }; db.BusinessPreferences.Add(busPref); //New business will also have default employee who created as a manager var emp = new Employee(); var email = HttpContext.Current.User.Identity.Name; emp.Id = Guid.NewGuid(); //Assign new ID on save. emp.BusinessLocation = buslocation; //Lookup user profile for authenicated user who is creating business emp.UserProfile = db.UserProfiles.Single(usr => usr.Email == email); emp.Email = emp.UserProfile.Email; emp.FirstName = emp.UserProfile.FirstName; emp.LastName = emp.UserProfile.LastName; emp.MobilePhone = emp.UserProfile.MobilePhone; emp.Type = EmployeeType.FullTime; //Default to Full time employee type emp.IsAdmin = true; emp.ManagerBusinessLocations.Add(buslocation); db.Employees.Add(emp); db.BusinessLocations.Add(buslocation); db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.Created, buslocation.Id)); } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } } else { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Unauthorized)); } }
public HttpResponseMessage PostBusiness(BusinessDetailsDTO businessDTO) { if (ModelState.IsValid) { //Check business with same name does not already exist: var existingBusiness = db.Businesses.Where(b => b.Name == businessDTO.Name).FirstOrDefault(); if (existingBusiness != null) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "A business already exists with the name '" + businessDTO.Name + "'")); } var business = MapperFacade.MapperConfiguration.Map <BusinessDetailsDTO, Business>(businessDTO); business.Id = Guid.NewGuid(); //Assign new ID on save. //Lookup BusinessType and attach, so that no updates or inserts are performed on BusinessType lookup table business.Type = db.BusinessTypes.Find(businessDTO.TypeId); //When creating new business if no business locations, must have one location by default if (business.BusinessLocations.Count == 0) { var busLoc = new BusinessLocation { Id = Guid.NewGuid(), Name = businessDTO.BusinessLocation.Name, Business = business, Address = new Address { Line1 = businessDTO.BusinessLocation.Address.Line1, Line2 = businessDTO.BusinessLocation.Address.Line2, Suburb = businessDTO.BusinessLocation.Address.Suburb, Postcode = businessDTO.BusinessLocation.Address.Postcode, State = businessDTO.BusinessLocation.Address.State, PlaceLatitude = businessDTO.BusinessLocation.Address.Lat, PlaceLongitude = businessDTO.BusinessLocation.Address.Long, PlaceId = businessDTO.BusinessLocation.Address.PlaceId }, Phone = businessDTO.BusinessLocation.Phone }; foreach (var intLoc in businessDTO.BusinessLocation.InternalLocations) { if (!String.IsNullOrEmpty(intLoc.Name)) { busLoc.InternalLocations.Add(new InternalLocation { Id = Guid.NewGuid(), Name = intLoc.Name, BusinessLocation = busLoc }); } } business.BusinessLocations.Add(busLoc); //Need to create a default preferences object BusinessPreferences busPref = new BusinessPreferences { Id = Guid.NewGuid(), CancelShiftAllowed = true, CancelShiftTimeframe = 0, BusinessLocation = busLoc }; db.BusinessPreferences.Add(busPref); //New business will also have default employee who created as a manager var emp = new Employee(); var email = HttpContext.Current.User.Identity.Name; emp.Id = Guid.NewGuid(); //Assign new ID on save. emp.BusinessLocation = busLoc; //Lookup user profile for authenicated user who is creating business emp.UserProfile = db.UserProfiles.Single(usr => usr.Email == email); emp.Email = emp.UserProfile.Email; emp.FirstName = emp.UserProfile.FirstName; emp.LastName = emp.UserProfile.LastName; emp.MobilePhone = emp.UserProfile.MobilePhone; emp.Type = EmployeeType.FullTime; //Default to Full time employee type emp.IsAdmin = true; emp.ManagerBusinessLocations.Add(busLoc); db.Employees.Add(emp); } db.Businesses.Add(business); try { db.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { System.Diagnostics.Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { System.Diagnostics.Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, business.Id); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = business.Id })); return(response); } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } }