コード例 #1
0
        public async Task <IActionResult> AddOpinion(int schoolId, OpinionForCreateDto opinionForCreateDto)
        {
            var school = await database.SchoolRepository.Get <School>(schoolId);

            int currentUserId = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (schoolService.UserOpinionExists(currentUserId, school))
            {
                return(BadRequest("Dla wybranej szkółki możesz dodać tylko jedną opinię"));
            }

            if (currentUserId == school.OwnerId)
            {
                return(this.Forbidden("Nie możesz dodać opinii dla swojej szkółki"));
            }

            var opinion = mapper.Map <Opinion>(opinionForCreateDto);

            if (await schoolService.AddOpinion(opinion, school, currentUserId))
            {
                var opinionToReturn = mapper.Map <OpinionDetailsDto>(opinion);

                await notificationSystem.PushNotification(school.OwnerId, StaticExpressions.OpinionCreatedForOwner(opinion.User.UserName), NotificationType.School);

                await notificationSystem.PushNotificationToUsersByFollows(StaticExpressions.OpinionCreated(opinion.User.UserName, school.Name), school);

                return(Ok(opinionToReturn));
            }

            return(BadRequest("Nie udało się dodać opinii"));
        }