public async Task <SessionDTO> CreateAsync(SessionCreateUpdateDTO session) { var response = await this.httpClient.PostAsJsonAsync("api/session", session); var result = JsonConvert.DeserializeObject <SessionDTO>(response.Content.ReadAsStringAsync().Result); return(result); }
public static SessionDTO ToSessionDto(SessionCreateUpdateDTO session) { return(new SessionDTO { Id = session.Id, Items = ToItemDtos(session.Items), SessionKey = session.SessionKey, Users = ToUserDtos(session.Users) }); }
public async Task Create_given_dto_creates_session() { var output = new SessionDTO(); var sessionRepo = new Mock <ISessionRepository>(); sessionRepo.Setup(s => s.CreateAsync(It.IsAny <SessionCreateUpdateDTO>())).ReturnsAsync(output); var controller = new SessionController(sessionRepo.Object, null, null); var input = new SessionCreateUpdateDTO(); await controller.Create(input); sessionRepo.Verify(s => s.CreateAsync(input)); }
public async Task <SessionDTO> CreateAsync(SessionCreateUpdateDTO session) { var entity = new Session { Items = EntityMapper.ToItemEntities(session.Items), Users = EntityMapper.ToUserEntities(session.Users), SessionKey = session.SessionKey }; this.context.Sessions.Add(entity); this.context.SaveChanges(); return(await this.FindAsync(entity.Id)); }
public async Task <bool> UpdateAsync(SessionCreateUpdateDTO session) { var entity = await this.context.Sessions.FindAsync(session.Id); if (entity == null) { return(false); } entity.Items = EntityMapper.ToItemEntities(session.Items); entity.Users = EntityMapper.ToUserEntities(session.Users); this.context.SaveChanges(); return(true); }
public async Task Create_given_dto_returns_CreatedAtActionResult() { var input = new SessionCreateUpdateDTO(); var output = new SessionDTO { Id = 42, SessionKey = "ABC1234" }; var sessionRepo = new Mock <ISessionRepository>(); sessionRepo.Setup(s => s.CreateAsync(input)).ReturnsAsync(output); var controller = new SessionController(sessionRepo.Object, null, null); var post = await controller.Create(input); var result = post.Value; Assert.Equal("ABC1234", result.SessionKey); Assert.Equal(42, result.Id); }
public async Task <ActionResult <SessionDTO> > Create([FromBody] SessionCreateUpdateDTO session) { var key = string.Empty; while (key == string.Empty) { var randomKey = StringUtils.RandomSessionKey(); if (await this.sessionRepository.FindByKeyAsync(randomKey) == null) { key = randomKey; } } session.SessionKey = key; session.Users = new List <UserCreateDTO>(); var created = await this.sessionRepository.CreateAsync(session); return(created); }
public async Task CreateAsync_sends_created() { var handler = new Mock <HttpMessageHandler>(); handler.Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.Created, Content = new StringContent(string.Empty) }) .Verifiable(); var httpClient = new HttpClient(handler.Object) { BaseAddress = this.baseAddress }; var client = new SessionClient(httpClient); var dto = new SessionCreateUpdateDTO { Id = 42, }; await client.CreateAsync(dto); handler.Protected().Verify( "SendAsync", Times.Once(), ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Post && req.RequestUri == new Uri("https://localhost:5001/api/session")), ItExpr.IsAny <CancellationToken>()); }
public async Task <bool> UpdateAsync(SessionCreateUpdateDTO session) { var response = await this.httpClient.PutAsJsonAsync($"api/session/{session.Id}", session); return(response.IsSuccessStatusCode); }