public void PutSubscribeUserToGame() { var controller = SetController(); var requestObject = new UserGameDTO() { GameId = 1, UserId = 1, }; controller.Validate(requestObject); var result = controller.PutSubscribeUserToGame(requestObject); Assert.IsNotNull(result.StatusCode); Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); requestObject = new UserGameDTO() { GameId = 1, UserId = 500, }; controller.Validate(requestObject); result = controller.PutSubscribeUserToGame(requestObject); Assert.IsNotNull(result.StatusCode); Assert.AreEqual(HttpStatusCode.NotFound, result.StatusCode); }
public HttpResponseMessage PutSubscribeUserToGame(UserGameDTO userGameDTO) { HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.InternalServerError); try { if (ModelState.IsValid) { UserBusiness userBusiness = new UserBusiness(); var result = userBusiness.SubscribeUserToGame(userGameDTO); if (result != null) { response = Request.CreateResponse(HttpStatusCode.OK, "User successfully subscribe to the game"); } else { response = Request.CreateResponse(HttpStatusCode.NotFound, "User not found"); } } else { response = Request.CreateResponse(HttpStatusCode.BadRequest, "The request object doesn't match with the requirement"); } } catch (Exception e) { Logger.Error("UserController", "Get", e.Message); response = Request.CreateResponse(HttpStatusCode.InternalServerError, "Unexpected error"); } return(response); }
public UserDTO SubscribeUserToGame(UserGameDTO userGameDTO) { Logger.Trace("UserBusiness", "SubscribeUserToGame", "Starting the method"); UserDTO result = null; try { var unitOfWork = Database.Context.Instance; var userRepository = new UserRepository(unitOfWork); var userGameRepository = new UserGameRepository(unitOfWork); var user = userRepository.GetById(userGameDTO.UserId); if (user != null) { user.Games = user.Games ?? new List <UserGame>(); //This will be always game 1 because I'm not implementing several games, but the code is //prepared to support it with minimal changes. var game = new Game() { Id = userGameDTO.GameId, Name = "Game" }; var userGame = new UserGame() { UserId = user.Id, User = user, GameId = 1, Game = game, Score = 0 }; user.Games.Add(userGame); userGameRepository.Create(userGame); user = userRepository.Update(user); result = UserToUserDTO(user); } } catch (Exception e) { Logger.Error("UserBusiness", "SubscribeUserToGame", e.Message); throw e; } finally { Logger.Trace("UserBusiness", "SubscribeUserToGame", "Finishing method"); } return(result); }
// POST api/gamerecord public HttpResponseMessage Post(GameDTO value) { DateTime pDate = DateTime.ParseExact(value.Date, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture); int idShesop = Int32.Parse(value.IdUserShesop); STEPPY_Gamification_Record newGameRecord = new STEPPY_Gamification_Record() { IdUserShesop = idShesop, Date = pDate, Mission1 = Decimal.Parse(value.Mission1), Mission2 = Decimal.Parse(value.Mission2), Mission3 = Decimal.Parse(value.Mission3), Mission4 = Decimal.Parse(value.Mission4), IdLevel = Int32.Parse(value.IdLevel), Point = Decimal.Parse(value.Point) }; container.STEPPY_Gamification_Record.Add(newGameRecord); container.SaveChanges(); //get game mission var mission = container.STEPPY_Gamification_Mission.ToList(); var missiondto = (from m in mission select new MissionDTO { IdLevel = m.IdLevel, Mission1 = m.Mission1, Mission2 = m.Mission2, Mission3 = m.Mission3, Mission4 = m.Mission4 }).ToList(); var userGame = new UserGameDTO() { dataLastGame = newGameRecord, mission = missiondto }; //return Request.CreateResponse(HttpStatusCode.Created, container.STEPPY_Gamification_Record.Where<STEPPY_Gamification_Record>(s => s.IdUserShesop == idShesop).ToList().Last()); return(Request.CreateResponse(HttpStatusCode.Created, userGame)); }
public IHttpActionResult Attend(UserGameDTO dto) { var userid = User.Identity.GetUserId(); var duplicate = context.UserGames.Any(u => u.AppUserId == userid && u.GameId == dto.GameId); if (duplicate) { return(BadRequest("Already exists.")); } var games = new UserGame { GameId = dto.GameId, AppUserId = userid }; context.UserGames.Add(games); context.SaveChanges(); return(Ok()); }
public void SubscribeUserToGame() { var userBusiness = new UserBusiness(); var requestObject = new UserGameDTO() { GameId = 1, UserId = 1, }; var result = userBusiness.SubscribeUserToGame(requestObject); Assert.IsNotNull(result); requestObject = new UserGameDTO() { GameId = 1, UserId = 500, }; result = userBusiness.SubscribeUserToGame(requestObject); Assert.IsNull(result); }