public async Task <bool> DeleteItem(int itemId) { using (_unitOfWork) { Item item = await _unitOfWork.ItemRepository.FindByID(itemId); User user = await _unitOfWork.UserRepository.GetUserWithItems(item.UserId); Trip trip = await _unitOfWork.TripRepository.GetTripWithItemsAndMembers(item.TripId); user.MyItems.Remove(item); trip.ItemList.Remove(item); _unitOfWork.UserRepository.Update(user); _unitOfWork.TripRepository.Update(trip); _unitOfWork.ItemRepository.Delete(itemId); Notification notification = new Notification(); notification.Seen = false; notification.RelatedObjectName = item.Name; notification.Type = NotificationType.RemovedItem; notification.User = user; notification.UserId = user.UserId; await _unitOfWork.NotificationRepository.Create(notification); await _unitOfWork.Save(); await _messageControllerService.NotifyOnTripChanges(trip.TripId, "RemoveItem", item.ItemId); NotificationItemDeleteDTO notificationItemDelete = new NotificationItemDeleteDTO() { Notification = _mapper.Map <Notification, NotificationDTO>(notification), ItemToDelete = item.ItemId }; await _messageControllerService.SendNotification(user.UserId, "RemoveItemNotification", notificationItemDelete); return(true); } }
public async Task <ItemDTO> ChangeUser(int ItemId, int newUserId) { using (_unitOfWork) { Item item = await _unitOfWork.ItemRepository.FindByID(ItemId); User user = await _unitOfWork.UserRepository.GetUserWithItems(item.UserId); Trip trip = await _unitOfWork.TripRepository.GetTripWithItemsAndMembers(item.TripId); User newUser = await _unitOfWork.UserRepository.GetUserWithItems(newUserId); if (!trip.Travelers.Contains(newUser)) { return(null); } user.MyItems.Remove(item); item.UserId = newUserId; item.User = newUser; if (newUser.MyItems == null) { newUser.MyItems = new List <Item>(); } newUser.MyItems.Add(item); _unitOfWork.UserRepository.Update(user); _unitOfWork.UserRepository.Update(newUser); _unitOfWork.ItemRepository.Update(item); Notification notification_old = new Notification(); notification_old.Seen = false; notification_old.RelatedObjectName = item.Name; notification_old.Type = NotificationType.RemovedItem; notification_old.UserId = user.UserId; await _unitOfWork.NotificationRepository.Create(notification_old); Notification notification_new = new Notification(); notification_new.Seen = false; notification_new.RelatedObjectName = item.Name; notification_new.Type = NotificationType.AddedItem; notification_new.UserId = newUserId; await _unitOfWork.NotificationRepository.Create(notification_new); await _unitOfWork.Save(); ItemDTO retItem = _mapper.Map <Item, ItemDTO>(item); await _messageControllerService.NotifyOnTripChanges(trip.TripId, "EditItem", retItem); NotificationItemDeleteDTO notificationItemDelete = new NotificationItemDeleteDTO() { Notification = _mapper.Map <Notification, NotificationDTO>(notification_old), ItemToDelete = item.ItemId }; await _messageControllerService.SendNotification(user.UserId, "RemoveItemNotification", notificationItemDelete); NotificationItemDTO notificationItem = new NotificationItemDTO() { Notification = _mapper.Map <Notification, NotificationDTO>(notification_new), Item = retItem }; await _messageControllerService.SendNotification(newUserId, "AddItemNotification", notificationItem); return(retItem); } }