public async Task InsertArmedTrap(ArmedTrapRequest request) { ArmedTrap armedTrap = Parse(request); armedTrap.Date = DateTime.UtcNow; armedTrap.Disarmed = false; AppRepository.EntitiesContext.ArmedTraps.Add(armedTrap); await AppRepository.EntitiesContext.SaveChangesAsync(); }
public async Task <IHttpActionResult> Arm(ArmedTrapApiRequest request) { request.UserId = (await GetUser()).Id; ArmedTrapRequest armedTrapRequest = Mapper.Map <ArmedTrapRequest>(request); UserResult user = await AppBusiness.Trap.ArmTrap(armedTrapRequest); UserApiResult response = Mapper.Map <UserApiResult>(user); return(Ok(response)); }
private ArmedTrap Parse(ArmedTrapRequest request) { if (request != null) { ArmedTrap response = new ArmedTrap(); response.Latitude = request.Latitude; response.Longitude = request.Longitude; response.NameKey = request.NameKey; response.User = AppRepository.EntitiesContext.Users.Find(new Guid(request.UserId)); return(response); } else { return(null); } }
public async Task <UserResult> ArmTrap(ArmedTrapRequest request) { bool isLocationValid = request.Latitude != 0 && request.Longitude != 0; UserResult user = null; if (isLocationValid) { TrapResult trap = await AppBusiness.Trap.GetByNameKey(request.NameKey); if (trap != null) { user = await AppBusiness.User.GetById(request.UserId); if (user != null) { if (user.Traps != null && user.Traps.Count > 0) { bool hasTrapToSet = false; foreach (UserTrapResult userTrap in user.Traps) { if (userTrap.NameKey == request.NameKey && userTrap.Amount > 0) { hasTrapToSet = true; break; } } if (hasTrapToSet) { request.Date = DateTime.UtcNow; await AppRepository.Trap.InsertArmedTrap(request); UserRequest userRequest = Mapper.Map <UserRequest>(user); await AppBusiness.User.RemoveTrap(userRequest, request.NameKey); user = await AppBusiness.User.GetById(request.UserId); } else { throw new MyTrapBusinessException(Resources.trap_armed_user_no_has); } } else { throw new MyTrapBusinessException(Resources.trap_armed_user_no_traps); } } } } if (!isLocationValid) { throw new MyTrapBusinessException(Resources.trap_armed_location_invalid); } return(user); }