public async Task <User> Handle(Query request, CancellationToken cancellationToken) { //handler logic //we get user object from usermanager-> httpcontext -> username var user = await _userManager.FindByNameAsync(_userAccess.GetCurrentUsername()); if (user == null) { throw new RestException(HttpStatusCode.Unauthorized); } return(new User { Token = _generator.CreateToken(user), DisplayName = user.DisplayName, Image = user.Photos.FirstOrDefault(x => x.IsMain)?.Url, Username = user.UserName }); }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { //handler logic var observer = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccess.GetCurrentUsername()); var target = await _context.Users.SingleOrDefaultAsync(x => x.UserName == request.Username); //observer will follow target user if (target == null) { throw new RestException(System.Net.HttpStatusCode.NotFound, new { User = "******" }); } //check if it is already followed or not var following = await _context.Followings.SingleOrDefaultAsync(x => x.ObserverId == observer.Id && x.TargetId == target.Id); if (following == null) { throw new RestException(System.Net.HttpStatusCode.BadRequest, new { User = "******" }); } else { _context.Followings.Remove(following); } var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem in saving data to database"); }
public async Task <Profile> ReadProfile(string username) { //return profile data with following info var user = await _dataContext.Users.SingleOrDefaultAsync(x => x.UserName == username); if (user == null) { throw new RestException(System.Net.HttpStatusCode.NotFound, new { User = "******" }); } var currentUser = await _dataContext.Users.SingleOrDefaultAsync(x => x.UserName == _userAccess.GetCurrentUsername()); var profile = new Profile { DisplayName = user.DisplayName, Username = user.UserName, Bio = user.Bio, Photos = user.Photos, Image = user.Photos.FirstOrDefault(x => x.IsMain)?.Url, FollowersCount = user.Followers.Count(), FollowingCount = user.Followings.Count() }; if (currentUser.Followings.Any(x => x.TargetId == user.Id)) { profile.isFollowed = true; } return(profile); }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { //handler logic var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccess.GetCurrentUsername()); //update user data user.DisplayName = request.DisplayName; user.Bio = request.Bio ?? user.Bio; var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem in saving details to database"); }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { //handler logic var activity = await _context.Activities.FindAsync(request.Id); if (activity == null) { throw new RestException(HttpStatusCode.NotFound, new { activity = "Not found" }); } var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccess.GetCurrentUsername()); //check for attendance of the currentuser and activity applied. var attendance = await _context.UserActivities.SingleOrDefaultAsync(x => x.ActivityId == activity.Id && x.AppUserId == user.Id); if (attendance != null) { throw new RestException(HttpStatusCode.BadRequest, new { activity = "already attending this event" }); } attendance = new UserActivity { Activity = activity, AppUser = user, IsHost = false, DateJoined = DateTime.Now }; _context.UserActivities.Add(attendance); var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem in saving activity to database"); }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { //handler logic var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccess.GetCurrentUsername()); var photo = user.Photos.FirstOrDefault(x => x.Id == request.Id); if (photo == null) { throw new RestException(HttpStatusCode.NotFound, new { Photo = "Not found" }); } if (photo.IsMain) { throw new RestException(HttpStatusCode.BadRequest, new { Photo = "already main photo" }); } //replace the ismain photo var mainPhoto = user.Photos.FirstOrDefault(x => x.IsMain); mainPhoto.IsMain = false; photo.IsMain = true; var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem in saving changes to database"); }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { var activity = new Activity { Id = request.Id, Title = request.Title, Description = request.Description, Category = request.Category, Date = request.Date, City = request.City, Venue = request.Venue }; _context.Activities.Add(activity); //get the current user and add to list var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccess.GetCurrentUsername()); var attendee = new UserActivity { AppUser = user, DateJoined = DateTime.Now, IsHost = true, Activity = activity }; _context.UserActivities.Add(attendee); var success = await _context.SaveChangesAsync() > 0; if (success) { //empty object that puts status 200 ok return(Unit.Value); } throw new Exception("Problem in saving activity to database"); }
public async Task <Photo> Handle(Command request, CancellationToken cancellationToken) { //handler logic var photoUploadResult = _photoAccessor.AddPhoto(request.file); var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccess.GetCurrentUsername()); var photo = new Photo { Url = photoUploadResult.Url, Id = photoUploadResult.PublicId }; //set this photo as main photo if not done yet if (!user.Photos.Any(x => x.IsMain)) { photo.IsMain = true; } //add to photos user.Photos.Add(photo); var success = await _context.SaveChangesAsync() > 0; if (success) { return(photo); } throw new Exception("Problem in saving photo to database"); }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { //handler logic var activity = await _context.Activities.FindAsync(request.Id); if (activity == null) { throw new RestException(HttpStatusCode.NotFound, new { activity = "Not found" }); } var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccess.GetCurrentUsername()); //check for attendance of the currentuser and activity applied. var attendance = await _context.UserActivities.SingleOrDefaultAsync(x => x.ActivityId == activity.Id && x.AppUserId == user.Id); if (attendance == null) { throw new RestException(HttpStatusCode.BadRequest, new { activity = "Not found" }); } //host cannot remove itself from the event if (attendance.IsHost) { throw new RestException(HttpStatusCode.NotFound, new { activity = "host cannot remove itself from this event" }); } //if event is there and user is not a host then simply remove _context.UserActivities.Remove(attendance); var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem in saving activity to database"); }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { //handler logic var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccess.GetCurrentUsername()); var photo = user.Photos.FirstOrDefault(x => x.Id == request.Id); if (photo == null) { throw new RestException(HttpStatusCode.NotFound, new { Photo = "Not found" }); } if (photo.IsMain) { throw new RestException(HttpStatusCode.BadRequest, new { Photo = "Cannot delete main profile photo" }); } //if photo is present with user call accessor and delete var result = _photoAccessor.DeletePhoto(photo.Id); if (result == null) { throw new Exception("Problem in deleting the photo"); } //remove photo from db user.Photos.Remove(photo); var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem in saving changes to database"); }
public async Task <ActivityEnvelope> Handle(Query request, CancellationToken cancellationToken) { //paging logic var querable = _context.Activities.Where(x => x.Date >= request.StartDate).OrderBy(x => x.Date).AsQueryable(); //check for isgoing where host will always go to the event if (request.IsGoing && !request.IsHost) { querable = querable.Where(x => x.UserActivities.Any(a => a.AppUser.UserName == _userAccess.GetCurrentUsername())); } //only get the hosted events if (request.IsHost && !request.IsGoing) { querable = querable.Where(x => x.UserActivities.Any(a => a.AppUser.UserName == _userAccess.GetCurrentUsername() && a.IsHost)); } var activities = await querable.Skip(request.Offset ?? 0).Take(request.Limit ?? 3).ToListAsync(); return(new ActivityEnvelope { Activities = _autoMapper.Map <List <Activity>, List <ActivityDto> >(activities), ActivityCount = querable.Count() }); }
public bool Resolve(UserActivity source, AtendeeDto destination, bool destMember, ResolutionContext context) { var currentUser = _dataContext.Users.SingleOrDefaultAsync(x => x.UserName == _userAccess.GetCurrentUsername()).Result; if (currentUser.Followings.Any(x => x.TargetId == source.AppUserId)) { return(true); } return(false); }