public HttpResponseMessage PutJoinBench(int benchId, string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { var context = new DrinkBenchContext(); using (context) { var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Invalid sessionkey"); } var benchesEntities = context.Benches; var bench = benchesEntities.FirstOrDefault(b => b.Id == benchId); if (bench == null) { throw new InvalidOperationException("Invalid bench id" + benchId); } bench.Users.Add(user); context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.OK); return(response); } }); return(responseMsg); }
public IQueryable <MessageModel> GetAll(string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { var context = new DrinkBenchContext(); var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Invalid sessionkey"); } var messageEntities = context.Messages; var models = (from messageEntity in messageEntities where messageEntity.User.Id == user.Id select new MessageModel() { id = messageEntity.Id, userId = messageEntity.User.Id, sendDate = messageEntity.SendDate, senderId = messageEntity.Sender.Id, Text = messageEntity.Text, Type = messageEntity.Type }); return(models.OrderByDescending(m => m.sendDate)); }); return(responseMsg); }
public List <FriendsModel> GetAll(string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { var context = new DrinkBenchContext(); var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Invalid sessionkey"); } var usersEntities = context.Users; var models = (from userEntity in usersEntities select new FriendsModel() { Id = userEntity.Id, StartTime = userEntity.StartTime, EndTime = userEntity.EndTime, Nickname = userEntity.Nickname, Avatar = userEntity.Avatar }); return(models.ToList()); }); return(responseMsg); }
public HttpResponseMessage PutLogout(string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new DrinkBenchContext(); using (context) { var user = context.Users.FirstOrDefault( usr => usr.SessionKey == sessionKey); if (user == null || user.SessionKey == null) { throw new InvalidOperationException("Invalid sessionkey"); } user.SessionKey = null; context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.OK); return(response); } }); return(responseMsg); }
public HttpResponseMessage PostRegister(RegisterModel model) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new DrinkBenchContext(); using (context) { //this.ValidateName(model.Firstname); //this.ValidateName(model.Lastname); this.ValidateAuthCode(model.AuthCode); var firstnameToLower = model.Firstname.ToLower(); var lastnameToLower = model.Lastname.ToLower(); var user = context.Users.FirstOrDefault( usr => usr.Firstname.ToLower() == firstnameToLower && usr.Lastname.ToLower() == lastnameToLower && usr.Nickname == model.Nickname); if (user != null) { throw new InvalidOperationException("Users exists"); } user = new User() { Avatar = model.Avatar, AuthCode = model.AuthCode, Firstname = model.Firstname, Lastname = model.Lastname, Nickname = model.Nickname, StartTime = DateTime.Now, EndTime = DateTime.Now }; context.Users.Add(user); context.SaveChanges(); user.SessionKey = this.GenerateSessionKey(user.Id); context.SaveChanges(); var loggedModel = new LoggedUserModel() { Firstname = user.Firstname, SessionKey = user.SessionKey, Id = user.Id }; var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel); return(response); } }); return(responseMsg); }
public HttpResponseMessage PostLogin(RegisterModel model) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new DrinkBenchContext(); using (context) { //this.ValidateName(model.Firstname); //this.ValidateName(model.Lastname); this.ValidateAuthCode(model.AuthCode); var firstnameToLower = model.Firstname.ToLower(); var user = context.Users.FirstOrDefault( usr => usr.Firstname.ToLower() == firstnameToLower && usr.AuthCode == model.AuthCode); if (user == null) { var userWithoutOAuth = context.Users.FirstOrDefault( usr => usr.Firstname.ToLower() == firstnameToLower && usr.Nickname == model.Nickname); if (userWithoutOAuth != null) { throw new InvalidOperationException( "Users info incorrect Firstname:" + model.Firstname + "AuthCode:" + model.AuthCode); } else { return(PostRegister(model)); } } if (user.SessionKey == null) { user.SessionKey = this.GenerateSessionKey(user.Id); context.SaveChanges(); } var loggedModel = new LoggedUserModel() { Firstname = user.Firstname, SessionKey = user.SessionKey, Id = user.Id }; var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel); return(response); } }); return(responseMsg); }
public UserModel GetById(int id, string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { var context = new DrinkBenchContext(); using (context) { var user = context.Users.FirstOrDefault(usr => usr.Id == id); var sessionUser = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); if (sessionUser == null) { throw new InvalidOperationException("Invalid sessionkey"); } var model = new UserModel() { Id = user.Id, Firstname = user.Firstname, Lastname = user.Lastname, Nickname = user.Nickname, StartTime = user.StartTime, EndTime = user.EndTime, Avatar = user.Avatar, Benches = user.Benches == null?null: (from bench in user.Benches select new BenchModel() { StartTime = bench.StartTime, EndTime = bench.EndTime, Id = bench.Id, Name = bench.Name, Latitude = bench.Latitude, Longitude = bench.Longitude, UsersCount = (bench.Users != null) ? bench.Users.Count : 0 }).ToList(), Friends = user.Friends == null?null: (from friend in user.Friends select new FriendsModel() { Id = friend.Id, StartTime = friend.StartTime, EndTime = friend.EndTime, Nickname = friend.Nickname, Avatar = friend.Avatar }).ToList() }; //this.GetAll(sessionKey).FirstOrDefault(u => u.Id == id); return(model); } }); return(responseMsg); }
public HttpResponseMessage PostAddFreind(string nickname, string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new DrinkBenchContext(); using (context) { if (sessionKey == null) { throw new ArgumentException("SessionKey is null!"); } var user = context.Users.FirstOrDefault( usr => usr.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException( "Users info incorrect SessienKey:" + sessionKey); } var hasFriend = user.Friends.FirstOrDefault( f => f.Nickname == nickname); if (hasFriend != null) { throw new InvalidOperationException( "Users are already set as friends"); } var friend = context.Users.FirstOrDefault( usr => usr.Nickname == nickname); Friend newFriend = new Friend() { Nickname = friend.Nickname, StartTime = friend.StartTime, EndTime = friend.EndTime, Avatar = friend.Avatar }; newFriend.Users.Add(user); context.Friends.Add(newFriend); user.Friends.Add(newFriend); context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.OK); return(response); } }); return(responseMsg); }
public BenchFullModel GetById(int id, string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { var context = new DrinkBenchContext(); var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Invalid sessionkey"); } var benchesEntities = context.Benches; var bench = benchesEntities.FirstOrDefault(b => b.Id == id); if (bench == null) { throw new InvalidOperationException("Invalid bench id" + id); } BenchFullModel model = new BenchFullModel() { Id = bench.Id, Name = bench.Name, Latitude = bench.Latitude, Longitude = bench.Longitude, StartTime = bench.StartTime, EndTime = bench.EndTime, Users = (from benchUser in bench.Users select new FriendsModel() { Id = benchUser.Id, Nickname = benchUser.Nickname, Avatar = benchUser.Avatar, StartTime = benchUser.StartTime, EndTime = benchUser.EndTime }).ToList() }; return(model); }); return(responseMsg); }
public List <BenchModel> GetAll(string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { var context = new DrinkBenchContext(); var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Invalid sessionkey"); } var benchesEntities = context.Benches; var models = (from benchEntity in benchesEntities select new BenchModel() { Id = benchEntity.Id, Name = benchEntity.Name, Latitude = benchEntity.Latitude, Longitude = benchEntity.Longitude, Users = (from benchUser in benchEntity.Users select new UserModel() { Id = benchUser.Id, Firstname = benchUser.Firstname, Lastname = benchUser.Lastname, Nickname = benchUser.Nickname, StartTime = benchUser.StartTime, EndTime = benchUser.EndTime, Avatar = benchUser.Avatar }), UsersCount = benchEntity.Users.Count, StartTime = benchEntity.StartTime, EndTime = benchEntity.EndTime }); return(models.ToList()); }); return(responseMsg); }
public HttpResponseMessage PostBenches(string sessionKey, BenchModel bench) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { var context = new DrinkBenchContext(); using (context) { var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Invalid sessionkey"); } Bench newBench = new Bench() { Latitude = bench.Latitude, Longitude = bench.Longitude, Name = bench.Name, StartTime = bench.StartTime, EndTime = bench.EndTime }; newBench.Users.Add(user); context.Benches.Add(newBench); context.SaveChanges(); CreatedBench createdBench = new CreatedBench() { Name = newBench.Name, Id = newBench.Id }; var response = this.Request.CreateResponse(HttpStatusCode.Created, createdBench); return(response); } }); return(responseMsg); }
public HttpResponseMessage PostMessage(MessageModel model, string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new DrinkBenchContext(); using (context) { var user = context.Users.FirstOrDefault( usr => usr.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Message user id incorret" + model.userId); } if (user.Id != model.userId) { throw new InvalidOperationException("Message user id and sessienKey don't match " + model.userId + " " + user.Id); } Message newMessage = new Message() { User = context.Users.FirstOrDefault( usr => usr.Id == model.userId), SendDate = model.sendDate, Sender = context.Users.FirstOrDefault( usr => usr.Id == model.senderId), Text = model.Text, Type = model.Type }; context.Messages.Add(newMessage); context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.OK); return(response); } }); return(responseMsg); }