public IActionResult AddNotification([FromBody] NotificationForCreationDto notificationFromBody) { if (!_userService.CheckIfUserExists(notificationFromBody.TargetUserId) || !_offerService.CheckIfOfferExists(notificationFromBody.OfferId)) { return(NotFound()); } if (notificationFromBody.Type == NotificationType.RentRequest) { if (!_offerService.CheckIfOfferIsActive(notificationFromBody.OfferId)) { return(BadRequest("Oferta nieaktywna.")); } else if (_notificationService.CheckIfUserAlreadySendRentRequest( notificationFromBody.TargetUserId, notificationFromBody.OfferId)) { return(BadRequest("Proœba o wypo¿yczenie dla tej oferty ju¿ zosta³a przes³ana.")); } else if (!_rentService.CheckIfUserHasEnoughPoints(notificationFromBody.TargetUserId)) { return(BadRequest("Niewystarczaj¹ca iloœæ punktów na zrealizowanie wypo¿yczenia.")); } } var offerToReturn = _notificationService.AddNotification(notificationFromBody); return(Ok(offerToReturn)); }
public NotificationDto AddNotification(NotificationForCreationDto notification) { var newNotification = Mapper.Map <Notification>(notification); _dbContext.Notifications.Add(newNotification); if (newNotification.Type == NotificationType.RentRequest) { var borrower = _dbContext.Users.FirstOrDefault(u => u.Id == notification.TargetUserId); _pointsService.ModifyPoints(borrower, new PointsModificationGetDepositStrategy()); } if (_dbContext.SaveChanges() == 0) { throw new Exception("Could not add notification"); } return(Mapper.Map <NotificationDto>(newNotification)); }