public async Task <IHttpActionResult> GetRatingTemplate(long addressId) { var tenantRole = await _context.Roles.FirstAsync(f => f.Name == "Tenant"); var tenantRoleRatingParams = await _context.RoleParameters.Where(r => r.RoleId == tenantRole.Id).ToListAsync(); var ratings = new List <RatingBreakdown>(); foreach (var tenantRoleRatingParam in tenantRoleRatingParams) { var roleParam = new RoleParameter(); EfMapper.Map(tenantRoleRatingParam, roleParam); var rating = new RatingBreakdown(); rating.RoleParameter = roleParam; rating.RoleParameterId = roleParam.Id; ratings.Add(rating); } var addressString = _context.Addresses.First(a => a.Id == addressId).AddressLine1; return(Ok( new PersonRatingViewModel { PersonRating = new PersonRating { RatingBreakdowns = ratings, DateCreated = DateTime.Today, DateUpdated = DateTime.Today, AddressId = addressId }, Address = addressString })); }
public async Task <IHttpActionResult> GetMyTenants(string hash) { var p = await GetMeIfIExist(); if (!p.AddressOwnerships.Any()) { return(Ok(new List <Person>())); } var myAddressesIds = p.AddressOwnerships.Select(a => a.AddressId); var myAddresses = _context.Addresses.Include(a => a.AddressOccupations).Include(a => a.AddressOwnerships).Where(a => myAddressesIds.Contains(a.Id)); var peopleWhoLivedAtMyAddress = myAddresses.SelectMany(a => a.AddressOccupations.Select(o => o.Person)); var people = await peopleWhoLivedAtMyAddress.Include(t => t.PersonAttributes).ToListAsync(); people = people.Select(EfMapper.Map).ToList(); foreach (var person in people) { var per = peopleWhoLivedAtMyAddress.First(pp => pp.Id == person.Id); var myAddress = per.AddressOccupations.First(a => myAddresses.Any(ad => ad.Id == a.AddressId)); person.AddressOccupations.Add(new AddressOccupation { Address = EfMapper.Map(myAddress.Address), OccupiedFrom = myAddress.OccupiedFrom, OccupiedTo = myAddress.OccupiedTo }); } return(Ok(people)); }
public async Task <IHttpActionResult> GetMyAddresses() { try { var me = await GetMyUserIdMappings(); var myaddressOwnerships = _context.AddressOwnerships.Where(a => a.PersonId == me.PersonId); var addresses = await myaddressOwnerships.Select(o => o.Address).ToListAsync(); var myAddresses = new List <Address>(); foreach (var address in addresses) { myAddresses.Add(EfMapper.Map(address)); } return(Ok(myAddresses)); } catch (Exception) { return(BadRequest()); } }
public async Task <IHttpActionResult> UpdateLandlord([FromBody] Person person) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { if (person.Id == 0) { throw new Exception("Error updating..."); } var dbPerson = await _context.People.FirstOrDefaultAsync(p => p.Id == person.Id); if (dbPerson == null) { return(NotFound()); } if (ModelState.IsValid) { foreach (var domainAspnetPersonMap in person.DomainAspnetPersonMaps) { if (domainAspnetPersonMap.Id == 0) { _context.DomainAspnetPersonMaps.AddOrUpdate(domainAspnetPersonMap); } if (domainAspnetPersonMap.Id != 0) { var dbObj = _context.DomainAspnetPersonMaps.First(o => o.Id == domainAspnetPersonMap.Id); EfMapper.Map(domainAspnetPersonMap, dbObj); } } await _context.SaveChangesAsync(); foreach (var address in person.AddressOwnerships.Select(a => a.Address)) { if (address.Id == 0) { _context.Addresses.AddOrUpdate(address); } if (address.Id != 0) { var dbAddress = _context.Addresses.First(a => a.Id == address.Id); EfMapper.Map(address, dbAddress); } } await _context.SaveChangesAsync(); foreach (var addressOccupation in person.AddressOccupations) { if (addressOccupation.Id == 0) { _context.AddressOccupations.AddOrUpdate(addressOccupation); } if (addressOccupation.Id != 0) { var dbObj = _context.AddressOccupations.First(o => o.Id == addressOccupation.Id); EfMapper.Map(addressOccupation, dbObj); } addressOccupation.PersonId = dbPerson.Id; } await _context.SaveChangesAsync(); foreach (var addressOwnership in person.AddressOwnerships) { if (addressOwnership.Id == 0) { _context.AddressOwnerships.AddOrUpdate(addressOwnership); } if (addressOwnership.Id != 0) { var dbObj = _context.AddressOwnerships.First(o => o.Id == addressOwnership.Id); EfMapper.Map(addressOwnership, dbObj); } addressOwnership.PersonId = dbPerson.Id; } EfMapper.Map(person, dbPerson); await _context.SaveChangesAsync(); } } catch (Exception) { return(NotFound()); } return(Ok()); }