Working on a web api restful service.
I have a table called tests. The primary key is auto-increment. Each row contains a username and a test question. This DB is not really optimised, but it doesn't need to be.
Because the primary key is just an int to keep each row unique, there are many rows with duplicate usernames. This is fine.
I want to be able to return all rows with a matching username.
I want to be able to do it with a get request with the url: www.mywebsite.com/api/tests/{username}
The default controller methods in Visual Studio are only able to search by primary key to return one unique result.
This is something like what I want, but it doesn't work. 500 error. I'm not knowledgeable on debugging either, so point me in the right direction there if possible so I can provide more info.
// GET: api/Tests/robert
[ResponseType(typeof(Test))]
public async Task<IHttpActionResult> GetTest(string id)
{
//This is the default approach that just searches by primary key
//Test test = await db.Tests.FindAsync(id);
/*This is roughly what I want. Compiles, but doesn't work.*/
var query = "SELECT* WHERE id=" + id;
Test test = db.Tests.SqlQuery(query, id);
if (test == null)
{
return NotFound();
}
return Ok(test);
}
Let me know where I screwed up. I've been blundering over this for hours. I don't claim to be particularly good at any of this.
Try declaring your method like this
[RoutePrefix("api")]
public class HisController : ApiController
{
[HttpGet]
public HttpResponseMessage Test(string name) {...}
}
Also, I would recommend You to use entity framework
[RoutePrefix("api")]
public class HisController : ApiController
{
[HttpGet]
public HttpResponseMessage Test(string name)
{
using (MyEntities bm = new MyEntities())
{
var usr = bm.Users.Where(u => u.Name == name ).ToList();
if (usr.Count() > 0)
return Request.CreateResponse(HttpStatusCode.OK, new
{
Success = true
,
Message = "Total users: " + usr.Count()
,
Data = usr
});
return Request.CreateResponse(HttpStatusCode.NotFound, new { Success = false, Message = "No users found..." });
}
}
}
Related
I'm writing a unit test for an API Controller performing delete action.
Here's the Delete Action
public IActionResult DeleteSubGenre(Guid subGenreId)
{
if (!_genreRepo.SubGenreExist(subGenreId))
{
return NotFound();
}
var genreObj = _genreRepo.SubGenre(subGenreId);
if (!_genreRepo.DeleteSubGenre(genreObj))
{
ModelState.AddModelError("", $"Something went wrong when deleting the record {genreObj.Name}");
return StatusCode(500, ModelState);
}
return NoContent();
}
The unit test for this action is written as
[Fact]
public void DeleteSubGenre_Returns_NoContentResult()
{
// Arrange
var subGenreRepositoryMock = new Mock<ISubGenreRepository>();
var subGenreIMapperMock = new MapperConfiguration(config =>
{
config.AddProfile(new MovieMapper());
});
var subGenreMapper = subGenreIMapperMock.CreateMapper();
SubGenresController subGenreApiController = new SubGenresController(subGenreRepositoryMock.Object, mapper: subGenreMapper);
var subGenreDto = new SubGenreDTO()
{
Name = "Adult Content",
DateCreated = DateTime.Parse("15 May 2015"),
Id = Guid.NewGuid(),
GenreId = Guid.NewGuid(),
Genres = new GenreDTO()
};
// Act
var subGenreResult = subGenreApiController.DeleteSubGenre(subGenreDto.Id);
var noContentResult = subGenreResult as NoContentResult;
// Assert
Assert.False(noContentResult.StatusCode is StatusCodes.Status204NoContent);
}
While debugging the test i noticed that subGenreResult was returning a status code of 404 instead of 204. I can seem to get a hang over it. I'll be glad to get plausible solution to this.
You have to setup your mock to drive the execution of your test case.
For example if you want to go through this line: if (!_genreRepo.SubGenreExist(subGenreId))
then you have to setup the following mock behaviour:
subGenreRepositoryMock.Setup(repo => repo.SubGenreExist(It.IsAny<Guid>)).Returns(true);
To reach this line: return NoContent(); you might need to setup the other two methods as well to drive your test case.
i am new to integration tests. I have a controller method which adds a user to the database, as shown below:
[HttpPost]
public async Task<IActionResult> CreateUserAsync([FromBody] CreateUserRequest request)
{
try
{
var command = new CreateUserCommand
{
Login = request.Login,
Password = request.Password,
FirstName = request.FirstName,
LastName = request.LastName,
MailAddress = request.MailAddress,
TokenOwnerInformation = User
};
await CommandBus.SendAsync(command);
return Ok();
}
catch (Exception e)
{
await HandleExceptionAsync(e);
return StatusCode(StatusCodes.Status500InternalServerError,
new {e.Message});
}
}
As you have noticed my method returns no information about the user which has been added to the database - it informs about the results of handling a certain request using the status codes. I have written an integration test to check is it working properly:
[Fact]
public async Task ShouldCreateUser()
{
// Arrange
var createUserRequest = new CreateUserRequest
{
Login = "testowyLogin",
Password = "testoweHaslo",
FirstName = "Aleksander",
LastName = "Kowalski",
MailAddress = "akowalski#onet.poczta.pl"
};
var serializedCreateUserRequest = SerializeObject(createUserRequest);
// Act
var response = await HttpClient.PostAsync(ApiRoutes.CreateUserAsyncRoute,
serializedCreateUserRequest);
// Assert
response
.StatusCode
.Should()
.Be(HttpStatusCode.OK);
}
I am not sure is it enough to assert just a status code of response returned from the server. I am confused because, i don't know, shall i attach to assert section code, which would get all the users and check does it contain created user for example. I don't even have any id of such a user because my application finds a new id for the user while adding him/her to the database. I also have no idea how to test methods like that:
[HttpGet("{userId:int}")]
public async Task<IActionResult> GetUserAsync([FromRoute] int userId)
{
try
{
var query = new GetUserQuery
{
UserId = userId,
TokenOwnerInformation = User
};
var user = await QueryBus
.SendAsync<GetUserQuery, UserDto>(query);
var result = user is null
? (IActionResult) NotFound(new
{
Message = (string) _stringLocalizer[UserConstants.UserNotFoundMessageKey]
})
: Ok(user);
return result;
}
catch (Exception e)
{
await HandleExceptionAsync(e);
return StatusCode(StatusCodes.Status500InternalServerError,
new {e.Message});
}
}
I believe i should somehow create a user firstly in Arrange section, get it's id and then use it in Act section with the GetUserAsync method called with the request sent by HttpClient. Again the same problem - no information about user is returned, after creation (by the way - it is not returned, because of my CQRS design in whole application - commands return no information). Could you please explain me how to write such a tests properly? Have i missed anything? Thanks for any help.
This is how I do it:
var response = (CreatedResult) await _controller.Post(createUserRequest);
response.StatusCode.Should().Be(StatusCodes.Status201Created);
The second line above is not necessary, just there for illustration.
Also, your response it's better when you return a 201 (Created) instead of the 200(OK) on Post verbs, like:
return Created($"api/users/{user.id}", user);
To test NotFound's:
var result = (NotFoundObjectResult) await _controller.Get(id);
result.StatusCode.Should().Be(StatusCodes.Status404NotFound);
The NotFoundObjectResult assumes you are returning something. If you are just responding with a 404 and no explanation, replace NotFoundObjectResult with a NotFoundResult.
And finally InternalServerErrors:
var result = (ObjectResult) await _controller.Get(id);
result.StatusCode.Should().Be(StatusCodes.Status500InternalServerError);
You can use integrationFixture for that using this NuGet package. This is an AutoFixture alternative for integration tests.
The documented examples use Get calls but you can do other calls too. Logically, you should test for the status code (OkObjectResult means 200) value and the response (which could be an empty string, that is no problem at all).
Here is the documented example for a normal Get call.
[Fact]
public async Task GetTest()
{
// arrange
using (var fixture = new Fixture<Startup>())
{
using (var mockServer = fixture.FreezeServer("Google"))
{
SetupStableServer(mockServer, "Response");
var controller = fixture.Create<SearchEngineController>();
// act
var response = await controller.GetNumberOfCharacters("Hoi");
// assert
var request = mockServer.LogEntries.Select(a => a.RequestMessage).Single();
Assert.Contains("Hoi", request.RawQuery);
Assert.Equal(8, ((OkObjectResult)response.Result).Value);
}
}
}
private void SetupStableServer(FluentMockServer fluentMockServer, string response)
{
fluentMockServer.Given(Request.Create().UsingGet())
.RespondWith(Response.Create().WithBody(response, encoding: Encoding.UTF8)
.WithStatusCode(HttpStatusCode.OK));
}
In the example above, the controller is resolved using the DI described in your Startup class.
You can also do an actual REST call using using Refit. The application is self hosted inside your test.
using (var fixture = new RefitFixture<Startup, ISearchEngine>(RestService.For<ISearchEngine>))
{
using (var mockServer = fixture.FreezeServer("Google"))
{
SetupStableServer(mockServer, "Response");
var refitClient = fixture.GetRefitClient();
var response = await refitClient.GetNumberOfCharacters("Hoi");
await response.EnsureSuccessStatusCodeAsync();
var request = mockServer.LogEntries.Select(a => a.RequestMessage).Single();
Assert.Contains("Hoi", request.RawQuery);
}
}
I have asp.net core web api with React client. I'm adding data through my user interface created in React. In my api, Db context is added as scoped service, and each time my request finishes and new one is started, all my data from previous request is lost.
This is how my Configure services looks like:
services.AddDbContext<TicketingContext>(o=>o.UseLazyLoadingProxies().UseSqlServer(connectionString));
Controller method for posting data looks like this:
[HttpPost("{id}/tickets")]
public IActionResult CreateNewTicket(string id,
[FromBody] TicketForCreationDto ticketForCreation)
{
if (ticketForCreation == null)
{
return BadRequest();
}
var ticketEntity = _mapper.Map<Ticket>(ticketForCreation);
_ticketRepository.AddNewTicket(ticketEntity);
_ticketRepository.AddTicketToClient(id, ticketEntity);
if (!_ticketRepository.Save())
{
throw new Exception("Creating ticket failed on save");
}
var ticketToReturn = _mapper.Map<TicketDto>(ticketEntity);
return CreatedAtRoute("GetTicket", new {id=id, ticketId = ticketToReturn.Id }, ticketToReturn);
}
and methods in repository like this:
AddNewTicket:
public void AddNewTicket(Ticket ticket)
{
if (ticket.Id == Guid.Empty)
{
ticket.Id = Guid.NewGuid();
}
var dispatcher = AssignTicketToDispatcher(ticket);
if (dispatcher == null)
{
throw new Exception("There are no dispatchers matching this ticket");
}
dispatcher.UserTickets.Add(new UserTicket()
{
IdentityUser = dispatcher,
Ticket = ticket,
UserId = dispatcher.Id,
TicketId = ticket.Id
});
_context.Tickets.Add(ticket);
}
AddTicketToClient:
public void AddTicketToClient(string id, Ticket ticket)
{
var client = _identityUserRepository.GetClient(id);
if (client == null)
{
client = _context.Users.Where(u => u.UserName == "username").FirstOrDefault();
}
client.UserTickets.Add(new UserTicket()
{
IdentityUser = client,
Ticket = ticket,
UserId = client.Id,
TicketId = ticket.Id
});
}
Save:
public bool Save()
{
return (_context.SaveChanges() >= 0);
}
I want to be able to store data gained through multiple requests.
Does anyone have idea how to do that?
Use the database as it's the best method you have for persisting your data.
So When you do a request - at the end of the request, after your latest data is saved - query for the data from previous requests that you need and return it.
e.g. retrieve the last 5 requests saved newest first (where id is example for your primary key field):
var latestSaved = _context.UserTickets.OrderByDescending(x => x.id).Take(5);
Or amend to return all relevant data for e.g. active user by passing a user id stored client side.
Pass through any params you need to request the relevant data.
Use joins / includes set up in your entities. Whatever you need to do - make use of your entity relationships to get what you need from you database. Why try and replicate what it already does? :)
I have a POST method which will return the list of items from the user and since I am very new to c# web api, I am having a hardtime putting the right condition and response if the Id is null, empty or invalid. I've tried similar response and it doesn't work mainly because those examples are using iHttpActionResult instead of List<>
here is the code in my controller which I am not sure what to place on the comments I provided:
[HttpPost]
public List<ValueStory> UserValueStories ([FromBody] ValueStory valuestory)
//public void UserValueStories([FromBody] ValueStory Id)
{
if (valuestory.Id == "" || valuestory.Id == null)
{
//what code to add to change status code to 400 and to display error message?
}
//what code to put if the id is not valid, what status code and what message?
var valueStoryName = (from vs in db.ValueStories
where vs.Id == valuestory.Id
select vs).ToList();
List<ValueStory> vs1 = new List<ValueStory>();
foreach (var v in valueStoryName)
{
vs1.Add(new ValueStory()
{
Id = v.Id,
ValueStoryName = v.ValueStoryName,
Organization = v.Organization,
Industry = v.Industry,
Location = v.Location,
AnnualRevenue = v.AnnualRevenue,
CreatedDate = v.CreatedDate,
ModifiedDate = v.ModifiedDate,
MutualActionPlan = v.MutualActionPlan,
Currency = v.Currency,
VSId = v.VSId
});
}
return vs1.ToList();
}
Appreciate some help and some directions on how to do this correctly.
Change your return type to IHttpActionResult.
To return a 400 BAD REQUEST, return BadRequest().
To return a 404 NOT FOUND, return NotFound().
To return your list data, return Ok(vs1).
See the documentation for more information.
Optional: If you are using a documentation tool like Swagger or the Web Api Help Pages, also add the [ResponseType(typeof(List<ValueStory>))] attribute to your action method.
Method would need to be updated to allow for that level of flexibility
[HttpPost]
[ResponseType(typeof(List<ValueStory>))]
public IHttpActionResult UserValueStories ([FromBody] ValueStory valuestory) {
if (valuestory.Id == "" || valuestory.Id == null) {
//what code to add to change status code to 400 and to display error message?
return BadRequest("error message");
}
var valueStoryName = (from vs in db.ValueStories
where vs.Id == valuestory.Id
select vs).ToList();
var vs1 = new List<ValueStory>();
foreach (var v in valueStoryName) {
vs1.Add(new ValueStory() {
Id = v.Id,
ValueStoryName = v.ValueStoryName,
Organization = v.Organization,
Industry = v.Industry,
Location = v.Location,
AnnualRevenue = v.AnnualRevenue,
CreatedDate = v.CreatedDate,
ModifiedDate = v.ModifiedDate,
MutualActionPlan = v.MutualActionPlan,
Currency = v.Currency,
VSId = v.VSId
});
}
return Ok(vs1);
}
If you really want to keep your return data type (which I think you shouldn't, do as stated in the other answer), then you can throw exceptions as described in Exception Handling in ASP.NET Web API:
To throw a simple exception with specific HTTP code use:
throw new HttpResponseException(HttpStatusCode.NotFound);
To specify message you can do as follows:
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(string.Format("No product with ID = {0}", id)), ReasonPhrase = "Product ID Not Found"
}
throw new HttpResponseException(resp);
As per the knowledge I have on Web API, in the POST method you have to return the result of the post call instead(or along with) of List.
Its better to create a new model which will store the data (List) and result of the POST call (error message and status code).
Based on the Id, you can add respective error message and code.
In case of invalid data, you can make data as null.
The model may look like this.
class Model{
string errorMsg,
string statusCode,
List<ValueStory> data
}
I have written a method in web api to partial update/patch a user. The code goes like this :
// PATCH API USER
[AcceptVerbs("PATCH")]
public HttpResponseMessage PatchDoc(int id, Delta<user> user)
{
user changeduser = db.users.SingleOrDefault(p => p.iduser == id);
if (changeduser == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
user.Patch(changeduser);
db.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK);
}
Its working fine if i am using updating/patching a string property like :
{location: "London,Uk"}
It works fine. But if i update/patch an integer property it doesnt makes changes, the field remains the same.
{profileviews: 43}
I even tried using json like
{profileviews: "43"}
But still no change. So what am i missing? Thanks.