public async Task <ResultWrapper <CompletedEvent> > CreateEvent(CompletedEvent newEvent, TokenDto token) { if (!TokenValidation.ValidateToken(token, _unitOfWork)) { List <ErrorResult> error = new List <ErrorResult>(); error.Add(new ErrorResult(401, "Invalid token.")); _unitOfWork.tokenRepo.Remove(_unitOfWork.tokenRepo.Find(tk => tk.token.Equals(token.AccessToken)).FirstOrDefault()); return(new ResultWrapper <CompletedEvent>(null, error)); } var errors = eventValid.ValidateEventCreation(newEvent); if (errors.Count() > 0) { return(new ResultWrapper <CompletedEvent>(null, errors)); } CompletedEvent existEvent = null; if (newEvent.EventId.HasValue) { existEvent = await GetEvent(newEvent.EventId.Value); } if (existEvent != null) { return(await UpdateEvent(existEvent, newEvent, token)); } Event ev = Mapper.Map <Event>(newEvent); Location location = Mapper.Map <Location>(newEvent.Location); // Insert a new location in DB _unitOfWork.LocationRepo.AddAsync(location); await _unitOfWork.LocationRepo.SaveAsync(); // Get user from token var userId = TokenValidation.GetUserIdFromToken(token, _unitOfWork); ev.Author = userId; ev.Location = location.LocationId; _unitOfWork.EventRepo.AddAsync(ev); await _unitOfWork.EventRepo.SaveAsync(); Category c = _unitOfWork.categoriesRepo.Find(e => e.Category1.Equals(newEvent.Category)).FirstOrDefault(); EventHasCategories ehc = new EventHasCategories(); ehc.EventId = ev.EventId; ehc.CategoryId = c.CategoryId; _unitOfWork.eventHasCategories.Add(ehc); await _unitOfWork.eventHasCategories.SaveAsync(); return(new ResultWrapper <CompletedEvent>(newEvent, null)); }
private async Task <ResultWrapper <CompletedEvent> > UpdateEvent(CompletedEvent existingEvent, CompletedEvent newEvent, TokenDto token) { var tokenOwnerId = TokenValidation.GetUserIdFromToken(token, _unitOfWork); if (tokenOwnerId != existingEvent.Author.UserId) { List <ErrorResult> error = new List <ErrorResult>(); error.Add(new ErrorResult(401, "Unauthorized operation.")); return(new ResultWrapper <CompletedEvent>(null, error)); } // Update Location var location = _unitOfWork.LocationRepo.Find(l => l.LocationId == existingEvent.Location.LocationId).FirstOrDefault(); location.Latitude = newEvent.Location.Latitude; location.Longitude = newEvent.Location.Longitude; location.Address = newEvent.Location.Address; await _unitOfWork.LocationRepo.SaveAsync(); // Update Event var eventToUpdate = _unitOfWork.EventRepo.Find(x => x.EventId == existingEvent.EventId).FirstOrDefault(); eventToUpdate.Description = newEvent.Description; eventToUpdate.StartDate = newEvent.StartDate; eventToUpdate.EndDate = newEvent.EndDate; eventToUpdate.Radius = newEvent.Radius; eventToUpdate.Name = newEvent.Name; await _unitOfWork.EventRepo.SaveAsync(); EventHasCategories evCategories = _unitOfWork.eventHasCategories.Find(evt => evt.EventId == newEvent.EventId).FirstOrDefault(); Category cf = _unitOfWork.categoriesRepo.Get(evCategories.CategoryId); if (!cf.Category1.Equals(newEvent.Category)) { Category newCate = _unitOfWork.categoriesRepo.Find(ct => ct.Category1.Equals(newEvent.Category)).FirstOrDefault(); _unitOfWork.eventHasCategories.Remove(evCategories); EventHasCategories newCategory = new EventHasCategories { CategoryId = newCate.CategoryId, EventId = evCategories.EventId }; _unitOfWork.eventHasCategories.Add(newCategory); await _unitOfWork.eventHasCategories.SaveAsync(); } return(new ResultWrapper <CompletedEvent>(newEvent, null)); }
public async Task <ResultWrapper <PagedResult <CompletedEvent> > > GetUserEvents(TokenDto token, int?currentPage) { int currIndexPage = currentPage == null ? 0 : currentPage.Value; if (token == null || !TokenValidation.ValidateToken(token, _unitOfWork)) { List <ErrorResult> errors = new List <ErrorResult>(); errors.Add(new ErrorResult(401, "Invalid token.")); return(new ResultWrapper <PagedResult <CompletedEvent> >(null, errors)); } int userId = TokenValidation.GetUserIdFromToken(token, _unitOfWork); var events = await _unitOfWork.EventRepo.GetEventsByUserAsync(userId); PagedResult <CompletedEvent> pg = new PagedResult <CompletedEvent>(currIndexPage, events.Count(), 25, Mapper.Map <IEnumerable <CompletedEvent> >(events)); return(new ResultWrapper <PagedResult <CompletedEvent> >(pg, null)); }
public async Task <ResultWrapper <IEnumerable <InviteOutDto> > > GetInvites(TokenDto token, int?eventId, int?state) { if (token == null || !TokenValidation.ValidateToken(token, _unitOfWork)) { List <ErrorResult> errors = new List <ErrorResult>(); errors.Add(new ErrorResult(401, "Invalid token.")); _unitOfWork.tokenRepo.Remove(_unitOfWork.tokenRepo.Find(tk => tk.token.Equals(token.AccessToken)).FirstOrDefault()); return(new ResultWrapper <IEnumerable <InviteOutDto> >(null, errors)); } int?userId = null; if (eventId == null) { userId = TokenValidation.GetUserIdFromToken(token, _unitOfWork); } var invites = _unitOfWork.InviteRepo.GetInvites(userId, eventId, state); return(new ResultWrapper <IEnumerable <InviteOutDto> >(Mapper.Map <IEnumerable <InviteOutDto> >(invites), null)); }
public async Task <bool> DeleteEvent(int eventId, TokenDto tk) { if (!TokenValidation.ValidateToken(tk, _unitOfWork)) { List <ErrorResult> error = new List <ErrorResult>(); error.Add(new ErrorResult(401, "Invalid token.")); _unitOfWork.tokenRepo.Remove(_unitOfWork.tokenRepo.Find(token => token.token.Equals(tk.AccessToken)).FirstOrDefault()); return(false); } Event deleteEvent = await _unitOfWork.EventRepo.GetAsync(eventId); var tokenAuthor = TokenValidation.GetUserIdFromToken(tk, _unitOfWork); if (deleteEvent.Author != tokenAuthor) { List <ErrorResult> error = new List <ErrorResult>(); return(false); } // Remove EventHasCategories evCategories = _unitOfWork.eventHasCategories.Find(evt => evt.EventId == deleteEvent.EventId).FirstOrDefault(); _unitOfWork.eventHasCategories.Remove(evCategories); await _unitOfWork.eventHasCategories.SaveAsync(); _unitOfWork.InviteRepo.RemoveRange(_unitOfWork.InviteRepo.Find((iv) => iv.EventId == deleteEvent.EventId)); await _unitOfWork.InviteRepo.SaveAsync(); _unitOfWork.LocationRepo.Remove(_unitOfWork.LocationRepo.Get(deleteEvent.Location.Value)); await _unitOfWork.LocationRepo.SaveAsync(); _unitOfWork.EventRepo.Remove(deleteEvent); await _unitOfWork.EventRepo.SaveAsync(); return(true); }