<commit_before>﻿using System;
using System.IO;
using System.Web.Mvc;
using System.Xml.Serialization;
using StorageLibrary;
using Tigwi_API.Models;

namespace Tigwi_API.Controllers
{
    public class ModifyAccountController : ApiController
    {
        //
        // POST : modifyaccount/write

        //[Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Write(MsgToWrite msg)
        {
            Error error;

            try
            {
                var accountId = msg.AccountId;
                //TODO: find how to test accountId
                //if (accountId == )
                //    accountId = Storage.Account.GetId(msg.AccountName);

                Storage.Msg.Post(accountId, msg.Message.Content);

                // Result is an empty error XML element
                error = new Error();
            }
            catch (StorageLibException exception)
            {
                // Result is an non-empty error XML element
                error = new Error(exception.Code.ToString());
            }

            var stream = new MemoryStream();
            (new XmlSerializer(typeof (Error))).Serialize(stream, error);

            return Content(stream.ToString());
        }

        //
        // POST : modifyaccount/delete

        //[Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(MsgToDelete msg)
        {
            Error error;

            try
            {
                //TODO: find out how to use this information, if necessary (?)
                var accountId = msg.AccountId;
                //TODO: find how to test accountId
                //if (accountId == )
                //    accountId = Storage.Account.GetId(msg.AccountName);

                Storage.Msg.Remove(msg.MessageId);

                // Result is an empty error XML element
                error = new Error();
            }
            catch (StorageLibException exception)
            {
                // Result is an non-empty error XML element
                error = new Error(exception.Code.ToString());
            }

            var stream = new MemoryStream();
            (new XmlSerializer(typeof(Error))).Serialize(stream, error);

            return Content(stream.ToString());
        }

        //
        // POST : /modifyaccount/suscribelist

        //[Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SubscribeList(SubscribeList subscribe)
        {
            Error error;

            try
            {
                var accountId = Storage.Account.GetId(subscribe.Account);

                Storage.List.Follow(subscribe.Subscription, accountId);

                // Result is an empty error XML element
                error = new Error();
            }
            catch (StorageLibException exception)
            {
                // Result is an non-empty error XML element
                error = new Error(exception.Code.ToString());
            }

            var stream = new MemoryStream();
            (new XmlSerializer(typeof (Error))).Serialize(stream, error);

            return Content(stream.ToString());
        }

        //
        // POST /modifyaccount/createlist

        //[Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateList(CreateList listCreation)
        {
            Error error;
<commit_msg>adding some comments about how to improve answer to create requests<commit_after>﻿using System;
using System.IO;
using System.Web.Mvc;
using System.Xml.Serialization;
using StorageLibrary;
using Tigwi_API.Models;

namespace Tigwi_API.Controllers
{
    public class ModifyAccountController : ApiController
    {
        //
        // POST : modifyaccount/write

        //[Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Write(MsgToWrite msg)
        {
            //TODO : Get back on this when Storage gives us a method to get a message with its Id to follow spec. Or we can build a new Content type that only provides Guid...
            Error error;

            try
            {
                var accountId = msg.AccountId;
                //TODO: find how to test accountId
                //if (accountId == )
                //    accountId = Storage.Account.GetId(msg.AccountName);

                Storage.Msg.Post(accountId, msg.Message.Content);

                // Result is an empty error XML element
                error = new Error();
            }
            catch (StorageLibException exception)
            {
                // Result is an non-empty error XML element
                error = new Error(exception.Code.ToString());
            }

            var stream = new MemoryStream();
            (new XmlSerializer(typeof (Error))).Serialize(stream, error);

            return Content(stream.ToString());
        }

        //
        // POST : modifyaccount/delete

        //[Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(MsgToDelete msg)
        {
            Error error;

            try
            {
                //TODO: find out how to use this information, if necessary (?)
               // var accountId = msg.AccountId;
                //TODO: find how to test accountId
                //if (accountId == )
                //    accountId = Storage.Account.GetId(msg.AccountName);

                Storage.Msg.Remove(msg.MessageId);

                // Result is an empty error XML element
                error = new Error();
            }
            catch (StorageLibException exception)
            {
                // Result is an non-empty error XML element
                error = new Error(exception.Code.ToString());
            }

            var stream = new MemoryStream();
            (new XmlSerializer(typeof(Error))).Serialize(stream, error);

            return Content(stream.ToString());
        }

        //
        // POST : /modifyaccount/suscribelist

        //[Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SubscribeList(SubscribeList subscribe)
        {
            Error error;

            try
            {
                var accountId = Storage.Account.GetId(subscribe.Account);

                Storage.List.Follow(subscribe.Subscription, accountId);

                // Result is an empty error XML element
                error = new Error();
            }
            catch (StorageLibException exception)
            {
                // Result is an non-empty error XML element
                error = new Error(exception.Code.ToString());
            }

            var stream = new MemoryStream();
            (new XmlSerializer(typeof (Error))).Serialize(stream, error);

            return Content(stream.ToString());
        }

        //
        // POST /modifyaccount/createlist

        //[Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateList(CreateList listCreation)
        {
            //TODO: change result so that we provide the informations of the list whet it's created. Spec to update.
            Error error;

{'fraction_non_alphanumeric': 0.0885686301944872, 'fraction_numerical': 0.0, 'mean_word_length': 1.4810829573064908, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3396173', 'n_tokens_mistral': 1775, 'n_tokens_neox': 1665, 'n_words': 467}