Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding visits and users #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/WebAPI/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using AskToniApi.Models;
using System.Linq;
using System;
using System.Threading.Tasks;

namespace AskToniApi.Controllers
{
[Route("api/[controller]")]
public class UserController : Controller
{
private readonly IUserRepository _userRepository;
public UserController(IUserRepository userRepository)
{
_userRepository = userRepository;
}

[HttpGet]
public Task<IEnumerable<User>> Get(int pageOffset = 0, int pageLimit = 0)
{
return GetUsers(pageOffset, pageLimit);
}

private async Task<IEnumerable<User>> GetUsers(int pageOffset, int pageLimit)
{
if (pageLimit > 0)
{
throw new NotImplementedException();
}
else
{
return await _userRepository.GetAllUsers();
}
}

// POST api/user
[HttpPost]
public void Post([FromBody]User value)
{
_userRepository.AddUser(new User()
{ FirstName = value.FirstName,
LastName = value.LastName});
}
}
}
88 changes: 88 additions & 0 deletions src/WebAPI/Controllers/VisitController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using AskToniApi.Models;
using System.Linq;
using System;
using System.Threading.Tasks;

namespace AskToniApi.Controllers
{
[Route("api/[controller]")]
public class VisitController : Controller
{
private readonly IVisitRepository _visitRepository;
public VisitController(IVisitRepository visitRepository)
{
_visitRepository = visitRepository;
}

[HttpGet]
public Task<IEnumerable<Visit>> Get(int pageOffset = 0, int pageLimit = 0, string restaurantMongoId = null)
{
return GetVisits(pageOffset, pageLimit, restaurantMongoId);
}

private async Task<IEnumerable<Visit>> GetVisits(int pageOffset, int pageLimit, string restaurantMongoId)
{
if (String.IsNullOrEmpty(restaurantMongoId) == false)
{
if (pageLimit > 0)
{
return await _visitRepository.GetAllVisitsToARestaurant(restaurantMongoId, pageOffset, pageLimit);
}
else
{
return await _visitRepository.GetAllVisitsToARestaurant(restaurantMongoId);
}
}
else
{
if (pageLimit > 0)
{
return await _visitRepository.GetVisitsUsingFilter(pageOffset, pageLimit);
}
else
{
return await _visitRepository.GetAllVisits();
}
}

}

// GET api/visit/ObjectId
[HttpGet("{id}")]
public Task<Visit> Get(string id)
{
return GetVisitByIdInternal(id);
}

private async Task<Visit> GetVisitByIdInternal(string id)
{
return await _visitRepository.GetVisit(id) ?? new Visit();
}

// POST api/visit
[HttpPost]
public void Post([FromBody]Visit value)
{
_visitRepository.AddVisit(new Visit()
{ RestaurantMongoId = value.RestaurantMongoId,
RestaurantYelpId = value.RestaurantYelpId,
VisitDate = value.VisitDate});
}

// PUT api/visit/ObjectId
[HttpPut("{id}")]
public void Put(string id, [FromBody]Visit value)
{
_visitRepository.UpdateVisit(id, value);
}

// DELETE api/visit/ObjectId
[HttpDelete("{id}")]
public void Delete(string id)
{
_visitRepository.RemoveVisit(id);
}
}
}
24 changes: 24 additions & 0 deletions src/WebAPI/Models/AskToniContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,29 @@ public IMongoCollection<Review> Reviews
return _db.GetCollection<Review>("reviews");
}
}

public IMongoCollection<Visit> Visits
{
get
{
return _db.GetCollection<Visit>("visits");
}
}

public IMongoCollection<User> Users
{
get
{
return _db.GetCollection<User>("users");
}
}

public IMongoCollection<UserVisit> UserVisits
{
get
{
return _db.GetCollection<UserVisit>("uservisits");
}
}
}
}
28 changes: 28 additions & 0 deletions src/WebAPI/Models/Interfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,32 @@ public interface IRecommendationRepository
Task<DeleteResult> RemoveReview(string id);
Task<ReplaceOneResult> UpdateReview(string id, Review item);

}

public interface IVisitRepository
{
Task<IEnumerable<Visit>> GetAllVisitsToARestaurant(string restaurantMongoId, int pageOffset, int pageLimit);

Task<IEnumerable<Visit>> GetAllVisitsToARestaurant(string restaurantMongoId);

Task<IEnumerable<Visit>> GetAllVisits();

Task<IEnumerable<Visit>> GetVisitsUsingFilter(int pageOffset, int pageLimit);

Task<Visit> GetVisit(string id);

Task AddVisit(Visit item);

Task<DeleteResult> RemoveVisit(string id);

Task<ReplaceOneResult> UpdateVisit(string id, Visit item);

Task AddUserToVisit(string userId, string visitId);
}

public interface IUserRepository
{
Task<IEnumerable<User>> GetAllUsers();

Task AddUser(User item);
}
16 changes: 16 additions & 0 deletions src/WebAPI/Models/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

public class User
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }

[BsonElement("FirstName")]
public string FirstName {get; set;}

[BsonElement("LastName")]
public string LastName {get; set;}
}
33 changes: 33 additions & 0 deletions src/WebAPI/Models/UserRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using AskToniApi.Models;
using MongoDB.Driver;
using MongoDB.Bson;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;
using Microsoft.Extensions.Options;

public class UserRepository : IUserRepository
{
private readonly AskToniContext _context = null;

public UserRepository(IOptions<DbConnectionConfig> dbConnectionConfig)
{
DbConnectionConfig _dbConnectionConfig = dbConnectionConfig.Value;
_context = new AskToniContext(_dbConnectionConfig.mLabConnectStr);
}

public async Task<IEnumerable<User>> GetAllUsers()
{
try {
return await _context.Users.Find(_ => true).ToListAsync();
}
catch (Exception ex) {
throw ex;
}
}

public async Task AddUser(User item)
{
await _context.Users.InsertOneAsync(item);
}
}
17 changes: 17 additions & 0 deletions src/WebAPI/Models/UserVisit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

public class UserVisit
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }

[BsonElement("VisitId")]
[BsonRepresentation(BsonType.ObjectId)]
public string VisitId {get; set;}

[BsonElement("UserId")]
public string UserId {get; set;}
}
19 changes: 19 additions & 0 deletions src/WebAPI/Models/Visit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

public class Visit
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }

[BsonElement("RestaurantMongoId")]
public string RestaurantMongoId {get; set;}

[BsonElement("RestaurantYelpId")]
public string RestaurantYelpId {get; set;}

[BsonElement("VisitDate")]
public MongoDB.Bson.BsonDateTime VisitDate {get; set;}
}
96 changes: 96 additions & 0 deletions src/WebAPI/Models/VisitRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using AskToniApi.Models;
using MongoDB.Driver;
using MongoDB.Bson;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;
using Microsoft.Extensions.Options;

public class VisitRepository : IVisitRepository
{
private readonly AskToniContext _context = null;

public VisitRepository(IOptions<DbConnectionConfig> dbConnectionConfig)
{
DbConnectionConfig _dbConnectionConfig = dbConnectionConfig.Value;
_context = new AskToniContext(_dbConnectionConfig.mLabConnectStr);
}

public async Task<IEnumerable<Visit>> GetAllVisitsToARestaurant(string restaurantMongoId, int pageOffset, int pageLimit)
{
try {
var filter = Builders<Visit>.Filter.Eq(v => v.RestaurantMongoId, restaurantMongoId);
return await _context.Visits.Find(filter).Skip(pageOffset*pageLimit).Limit(pageLimit).ToListAsync();
}
catch (Exception ex) {
throw ex;
}
}

public async Task<IEnumerable<Visit>> GetAllVisitsToARestaurant(string restaurantMongoId)
{
try {
var filter = Builders<Visit>.Filter.Eq(v => v.RestaurantMongoId, restaurantMongoId);
return await _context.Visits.Find(filter).ToListAsync();
}
catch (Exception ex) {
throw ex;
}
}

public async Task<IEnumerable<Visit>> GetAllVisits()
{
try {
return await _context.Visits.Find(_ => true).ToListAsync();
}
catch (Exception ex) {
throw ex;
}
}

public async Task<IEnumerable<Visit>> GetVisitsUsingFilter(int pageOffset, int pageLimit)
{
try {
return await _context.Visits.Find(_ => true).Skip(pageOffset*pageLimit).Limit(pageLimit).ToListAsync();
}
catch (Exception ex) {
throw ex;
}
}

public async Task<Visit> GetVisit(string id)
{
var filter = Builders<Visit>.Filter.Eq(r => r.Id, id);
return await _context.Visits
.Find(filter)
.FirstOrDefaultAsync();
}

public async Task AddVisit(Visit item)
{
await _context.Visits.InsertOneAsync(item);
}

public async Task<DeleteResult> RemoveVisit(string id)
{
return await _context.Visits.DeleteOneAsync(
Builders<Visit>.Filter.Eq(r => r.Id, id));
}

public async Task<ReplaceOneResult> UpdateVisit(string id, Visit item)
{
item.Id = id;

return await _context.Visits
.ReplaceOneAsync(r => r.Id.Equals(ObjectId.Parse(id)),
item,
new UpdateOptions { IsUpsert = true });
}

public async Task AddUserToVisit(string userId, string visitId)
{
var userVisit = new UserVisit(){ UserId = userId,
VisitId = visitId};
await _context.UserVisits.InsertOneAsync(userVisit);
}
}