public async Task <bool> CreateLaboratoryAsync(CreateLaboratoryRequest request) { if (request.Name.Length == 0) { throw new RequestError("Name can not be empty"); } if (request.Name.Length > 50) { throw new RequestError("Name can not be longer than 50"); } if (request.Latitude > 42.37 || request.Latitude < 40.85 || request.Longitude < 20.45 || request.Longitude > 23.02) { throw new RequestError("Coordinate is not in Macedonia"); } if (request.City.Length == 0) { throw new RequestError("City name can not be empty"); } Cities city; if (!Enum.TryParse(request.City, out city)) { throw new RequestError("City name not valid"); } List <ContactInformation> ci = new List <ContactInformation>(); foreach (CreateContactInformationRequest req in request.ContactInformation) { string res = req.Validate(); if (res != "") { throw new RequestError(res); } var temp = new ContactInformation { Content = req.Content, Type = Enum.Parse <ContactInformationType>(req.Type, true) }; unitOfWork.ContactInformation.Add(temp); ci.Add(temp); } Organisation organisation = await unitOfWork.Organisation.Where(o => o.Id == request.OrganisationId).SingleOrDefaultAsync(); if (organisation == default(Organisation)) { throw new RequestError("Non existing organisation"); } // AUTHORISATION CHECK int myId = authService.CurrentUser.Id; if (myId != organisation.Director.Id && organisation.Permissions.Where(p => p.UserId == myId).SingleOrDefault() == default(OrganisationPermission)) { throw new RequestError(403, "Permission denied!"); } User coordinator = await unitOfWork.Users.Where(u => u.Id == request.CoordinatorId).SingleOrDefaultAsync(); if (coordinator == default(User)) { throw new RequestError("Non-existing coordinator"); } List <User> team = new List <User>(); foreach (int teamMember in request.Team) { var user = await unitOfWork.Users.Where(u => u.Id == teamMember).SingleOrDefaultAsync(); if (user == default(User)) { throw new RequestError("Team member does not exist!"); } team.Add(user); } Laboratory lab = new Laboratory { Name = request.Name, Description = request.Description, Organisation = organisation, Coordinator = coordinator, Longitude = request.Longitude, Latitude = request.Latitude, City = city, Visits = 0, ContactInformation = ci, }; List <LaboratoryTeam> teamMembers = team.Select(t => new LaboratoryTeam { Laboratory = lab, User = t }).ToList(); lab.Team = teamMembers; unitOfWork.Laboratories.Add(lab); await unitOfWork.SaveAsync(); return(true); }
public async Task <bool> CreateLaboratoryAsync([FromBody] CreateLaboratoryRequest request) { return(await laboratoryService.CreateLaboratoryAsync(request)); }