Пример #1
0
        public async Task <IActionResult> Create([FromBody] CalendarRequestModel model)
        {
            (await _calendarValidator.ValidateCreateAsync(model, this.GetUserId())).ToModelState(ModelState);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.ToStringEnumerable()));
            }

            var domainCalendar = _mapper.Map <Calendar>(model);
            var id             = await _calendarService.CreateCalendarAsync(domainCalendar, this.GetUserId());

            return(Ok(new { Id = id }));
        }
Пример #2
0
        public async Task <IActionResult> Import(CalendarImportModel model)
        {
            // TODO: Handle exceptions.

            if (!model.File.ContentType.Equals(ICAL_MIME))
            {
                ModelState.AddModelError("File", $"Content-Type \"{model.File.ContentType}\" is not supported.");
            }

            var userId = this.GetUserId();

            (await _calendarValidator.ValidateCreateAsync(model, userId)).ToModelState(ModelState);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.ToStringEnumerable()));
            }

            string fileContent = null;

            try
            {
                using (var reader = new StreamReader(model.File.OpenReadStream()))
                {
                    fileContent = await reader.ReadToEndAsync();
                }
            }
            catch
            {
                return(this.BadRequestError("Unable to read file."));
            }

            if (string.IsNullOrEmpty(fileContent))
            {
                return(this.BadRequestError("Unable to read file."));
            }

            Calendar calendar = null;

            try
            {
                calendar = _calendarImportService.ConvertIntoDomain(_calendarImportService.DeserializeCalendar(fileContent));
            }
            catch
            {
                return(this.BadRequestError("Unable to deserialize file."));
            }

            calendar.Owner = new User {
                Id = userId
            };
            calendar.Name        = model.Name;
            calendar.Description = model.Description;
            calendar.Color       = model.Color;

            var guid = Guid.NewGuid().ToString();

            _cacheService.SetItem(guid, calendar, 5);

            return(Ok(new
            {
                Key = guid,
                Calendar = calendar
            }));
        }